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

本文关键字:元素 属性 linq 使用 xml 文件 读取 | 更新日期: 2023-09-27 18:15:40

我已经能够使用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,但我不知道如何。

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

这可能就是你要找的语句。

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
}

假设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
    }