# Iteration over an XDocument based on some existing elements and conditions

I wrote this piece of code which works as expected:

XElement theContentWhichIncludeBuilds = xDocument.XPathSelectElement(\$"//Project/Group").Elements()
.FirstOrDefault(e => e.Name== "Content" && e.Attribute("Included").Value == "buildings");


It targets all the Group elements under the Project nodes,
where its Content has an Include attribute with a value of buildings.

The base sample XML:

<Project>
<Group>
<Content Include="buildings"/>


I need to use the provided code on top in 2,3 places in my project, the problem with it is that some XML files that I will process will not have that structure, and I am checking if it has that elements based on the condition or not.

Obviously it may cause errors when it doesn't have that element or the needed architecture. So I need to put it in Try/Catch.

Isn't it better to check each level one-by one and if that existed then check for the next node? (That way also avoid using try/catch) Does anyone confirm the used method can be good or bad on this case, or can provide any better alternative method for it?

Edit:

The XML files could be similar to the .CSProj structure which I altered a bit for my need. basically, here we may have:

<Project>
Basic data under the project node, which might be the same in all documents
</Project>


only some may have:

  <Group>
<Content Include="buildings"/>


Now we want to check if it doesn't have the missed part we be able to add it, or if it contained the part we may alter its' values.

It is an upgrade process of those documents.

• Have you considered to Descendants("xyz").Any() to check element's existence? – Peter Csala Jul 15 at 9:17
• No didn't try, what is different, I am kind of new to XML iteration, found that there could be tons of ways for doing anything, at first liked the approach I used, cause it was just a single line, but now I am trying to consider other approaches and ideas, if you think it may has some benefits over my provided code, please provide your suggestion as some piece of code and tell me what differs here. Thanks. – Kasrak Jul 15 at 9:21
• Guess your approach may not cause the NullException if I am right. How can I change this code to your suggested approach? – Kasrak Jul 15 at 9:23
• No it will cause any exception. The Descendants, Elements and Attributes methods return those items where the predicate is met. If there is no match, then it will return an empty collection. The Any extension method checks the length of the collection. If it is zero then it returns false otherwise true. – Peter Csala Jul 15 at 9:35
• That's probably a fine way. If possible, could you provide that piece of code as an answer, so I be able to mark it as the answer. – Kasrak Jul 15 at 9:36

There are several ways how you can process an XML using Linq2XML.

As I can understand you want to process only those XML files that are matching a given format. You can check this with XML native tools, like XSD or DTD schema definitions and validation or manually via XLinq.

If you want to check that with XLinq I would suggest to use the following methods: Descendants, Elements and Attributes. Each returns those items where the predicate is met. If there is no match, then it will return an empty collection.

If you combine this logic with the Any extension method then you can turn your query operators into existence operators. If the queried collection's length is zero then it returns false otherwise true.

I would suggest to break your logic into two sub-functions:

• Check whether or not the given xml meets the desired format
• Process only that xml, which satisfies the preconditions

EDIT: To reflect new requirements

Check the presence of the Group element and act based on its existence:

var xml = "<Project><Group><Content Include = \"buildings\" /></Group></Project>";
var xDocument = XDocument.Parse(xml);
var project = xDocument.Root;
var isGroupExist = project.Elements("Group").Any();
if (!isGroupExist)
{
var group = new XElement("Group") { ... };