简体   繁体   English

如何从xml文件中读取变量子节点?

[英]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. 我需要解析一个xml配置文件,其中包含可变数量的节点,这些节点又包含可变数量的子节点。 How do I read in all the values no matter how many VM nodes or vmClient nodes I have? 无论我有多少个VM节点或vmClient节点,我如何读入所有值? I'm using C# with VS2012 for this project. 我在这个项目中使用C#和VS2012。 As an aside, if there's a better way to structure my data in the xml file, let me know! 顺便说一句,如果有更好的方法来构建xml文件中的数据,请告诉我! Here's the xml file: 这是xml文件:

<!--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 我建议你使用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. 前缀vm对我来说有点奇怪。 Why not simply use language if it's part of vm data? 如果它是vm数据的一部分,为什么不简单地使用language 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");

            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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