I want to read a XML hierarchy into a tree of in-memory objects. The XML tree could have n-levels of children. I do not know the exact number. My in-memory objects have a children and parent property to be bound to a tree control.
How can I read xml file/string in a generic way into my in-memory objects when I do not know how the xml element tags are called/written exactly?
For example someone could provide me a xml structure of units where each unit has many units etc… so I know the xml tag is “unit” but it could be “module” too or anything else… it must work generic but I do not what to ask the user entering the xml element tag name like “unit”.
Is that possible at all what I want to achieve?
Either way, you have to know the node name to parse the hierarchy at least you have to have a definition. IMHO,
XElementis the only sort of genericXMLparser around. For instance, you have a XML like this:As I said earlier, you should have some definitions like the
root nodemust the hierarchical element name and the children container must beroot node name + s. This is one simple way that you can allow your users to specify any node names they wish but with some constraints.You may parse the
XMLusingXElementlike this:And of course you can
LinqthexElementsand to parse the hierarchy you can recur to build your tree control.You may take a kick start on xElement with these links below:
Hope this helped.