1

Dynamic may not be the best use of the word here, but I will give an example of what I mean:

I have some configuration files in XML format and I need to access certain elements of these configuration files. For now, in code, I am doing something like:

string serverName = elements.Element("Server").Value;

The above is all well and good if Server remains as a child element of elements, but if it moves, I would have to modify the code. One solution I thought of was to use XPATH expressions defined in some file that I would load at runtime and if the element or attribute that I was looking for changed, I would not have to change code, I would just have to change the XPATH expression. Is this a viable approach or is there another solution to this?

1 Answer 1

1

You can use Descendants to find any element "Server" element in the tree. However changing the code when Xml changes may not be a bad thing - at least you know what you are reading. If you use Descendands you would read the value regardless of whether the change was intentional or not meaning you read any Xml document that happens to have "Server" element inside, meaning that the Xml files you are accepting do not have to adhere to any structure/definitione (unless you have xsd to enforce it)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.