简体   繁体   English

读取xml文件:使用linq从元素属性解析

[英]Reading xml file: parsing from element attributes using linq

I've been able to create an xml file using linq, using this code: 我已经能够使用linq创建一个xml文件,使用以下代码:

    XElement config =
        new XElement("Configurations",
        new XElement("configuration",
            new XAttribute("mode", 1),
            new XElement("Platform",
                new XAttribute("name", "Device Portal")),
            new XElement("IPConfigurations",
                new XAttribute("IPmode", eS_IPsettings1.IPMode),
                new XAttribute("IPAddress", eS_IPsettings1.IPAddress),
                new XAttribute("NetMask", eS_IPsettings1.NetMask),
                new XAttribute("GateWay", eS_IPsettings1.Gateway)),
            new XElement("ProxyConfigurations",
                new XAttribute("ProxyMode", eS_ProxySettings1.Enable),
                new XAttribute("ProxyURL", eS_ProxySettings1.ProxyURL)),
                new XElement("KeyLockConfigurations",
                    new XAttribute("KeyLockMode", eS_KeyLock1.Enable),
                    new XAttribute("Pin", eS_KeyLock1.Pin))
            )
        );

which produces xml files like this: 它生成像这样的xml文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<Configurations>
  <configuration mode="1">
    <Platform name="Device Portal" />
    <IPConfigurations IPmode="Keep existing" IPAddress="..." NetMask="..." GateWay="..." />
    <ProxyConfigurations ProxyMode="Keep existing" ProxyURL="Enter proxy URL here" />
    <KeyLockConfigurations KeyLockMode="Keep existing" Pin="" />
  </configuration>
</Configurations>

Now I want to check the attribute value of configuration , and based if the value is 1, I want to parse the attribute values of the child elements in this configuration. 现在我想检查configuration的属性值,并且如果值为1,我想解析此配置中子元素的属性值。 What is the best approach to do so? 这样做的最佳方法是什么?

I've tried it using LoadXml, but I couldn't figure out how to make that work... I think the best way to read the file is using linq but i have no clue how. 我已经尝试使用LoadXml,但我无法弄清楚如何使这项工作......我认为阅读文件的最佳方法是使用linq,但我不知道如何。

This is probably the statement you're looking for. 这可能是您正在寻找的陈述。

Config.Descendants("configuration").Where(xel=>xel.Attribute("mode").Value==1)

Based on how complex the processing is, you can consider putting it in a foreach loop. 根据处理的复杂程度,您可以考虑将其置于foreach循环中。 Like this: 像这样:

foreach (var element in Config.Descendants("configuration").Where(xel=>xel.Attribute("mode").Value==1))
{
   //handle element
}

Assuming xml is in the string or this can come from file or any other stream; 假设xml在字符串中,或​​者这可以来自文件或任何其他流; you can load it in XDocument and use linq to find your nodes and attributes. 您可以在XDocument中加载它并使用linq查找您的节点和属性。

    string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
    <Configurations>
    <configuration mode=""1"">
        <Platform name=""Device Portal"" />
        <IPConfigurations IPmode=""Keep existing"" IPAddress=""..."" NetMask=""..."" GateWay=""..."" />
        <ProxyConfigurations ProxyMode=""Keep existing"" ProxyURL=""Enter proxy URL here"" />
        <KeyLockConfigurations KeyLockMode=""Keep existing"" Pin="""" />
    </configuration>
</Configurations>";

    using (var strm = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(xml)))
    {
        var doc = XDocument.Load(strm);
        var configs = doc.Nodes()
            .Where(x => (x is XElement) && ((XElement)x).Name == "Configurations")
            .Cast<XElement>();

        var firstConfig = configs
            .FirstOrDefault()
            .Nodes()
            .FirstOrDefault(x => (x is XElement) && ((XElement)x).Name == "configuration")
            as XElement;

        var mode = firstConfig.Attributes().FirstOrDefault(a => a.Name == "mode");
      //mode now has Value of "1".
      //access it as mode.Value
    }

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

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