简体   繁体   中英

How to read variable child nodes from an xml file?

I need to parse an xml config file with a variable number of nodes which in turn contain a variable number of child nodes. How do I read in all the values no matter how many VM nodes or vmClient nodes I have? I'm using C# with VS2012 for this project. As an aside, if there's a better way to structure my data in the xml file, let me know! Here's the xml file:

<!--VM settings on ESX Server-->
<VM name="DE2008">
    <vmLanguage value = "de"/>
    <vmSnapshot value = "test"/>
    <vmPowerOn value = "true"/>
    <vmClients>
        <vmClient name="ITXPPRO">
            <vmClientSnapshot value="test2"/>
            <vmClientLanguage value="it"/>
        </vmClient>
        <vmClient name="JPXPPRO">
            <vmClientSnapshot value="test3"/>
            <vmClientLanguage value="jp"/>
        </vmClient>
    </vmClients>
</VM>

<VM name="FR2003">
    <vmLanguage value = "fr"/>
    <vmSnapshot value = "test"/>
    <vmPowerOn value = "true"/>
    <vmClients>
        <vmClient name="DAWIN7">
            <vmClientSnapshot value="autotc"/>
            <vmClientLanguage value="da"/>
        </vmClient>
    </vmClients>
</VM>

<!--SQL Servers used as database servers for ePO-->
<sqlServers>
    <sqlName value = "DE2008SQL"/>
    <sqlLanguage value = "de"/>
    <sqlSnapshot value = "sql"/>
</sqlServers>

I suggest you to use LINQ to XML

var xdoc = XDocument.Load(path_to_xml);
var query = from vm in xdoc.Descendants("VM")
            select new {
               Name = (string)vm.Attribute("name"),
               Language = (string)vm.Element("vmLanguage").Attribute("value"),
               Snapshot = (string)vm.Element("vmSnapshot").Attribute("value"),
               PowerOn = (bool)vm.Element("vmPowerOn").Attribute("value"),
               Clients = from c in vm.Element("vmClients").Elements()
                         select new {
                            Name = (string)c.Attribute("name"),
                            // etc
                         }
            };

Also I suggest you to store values directly as element values, or in attributes, so instead of

Language = (string)vm.Element("vmLanguage").Attribute("value")

You will be able to use (also you will not get exception if some element missing)

Language = (string)vm.Element("vmLanguage") // if value stored in element
// or
Language = (string)vm.Attribute("vmLanguage") // if value stored in attribute

And prefix vm looks kind of weird to me. Why not simply use language if it's part of vm data? So, I'd go with following configuration:

<vm name="DE2008" language="de" shapshot="test" powerOn="true">
    <clients>
        <client name="ITXPPRO" snapshot="test2" language="it"/>
        <client name="ITXPPRO" snapshot="test3" language="jp"/>
    </clients>
</vm>

your just use something like this

    XDocument xDocument = XDocument.Load(yourconfigfilepath);


            var firstresult = xDocument.Descendants("VM");
foreach (var elt in result)
            {
                var childforeachNode = xDocument.Descendants("vmClient");

            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM