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

时间:2013-05-27 20:48:55

标签: c# xml linq

我已经能够使用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))
            )
        );

生成这样的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>

现在我想检查configuration的属性值,如果值为1,我想解析此配置中子元素的属性值。这样做的最佳方法是什么?

我已经尝试过使用LoadXml,但我无法弄清楚如何使其工作......我认为阅读文件的最佳方法是使用linq,但我不知道如何。

2 个答案:

答案 0 :(得分:2)

这可能是您正在寻找的陈述。

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

根据处理的复杂程度,您可以考虑将其置于foreach循环中。 像这样:

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

答案 1 :(得分:0)

假设xml在字符串中,或​​者这可以来自文件或任何其他流;您可以在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
    }
相关问题