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
Content has an
Include attribute with a value of
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?
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.