2pietjuh2
2pietjuh2

Reputation: 879

Reading xml file: parsing from element attributes using linq

I've been able to create an xml file using linq, using this code:

    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 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. 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.

Upvotes: 0

Views: 707

Answers (2)

loopedcode
loopedcode

Reputation: 4893

Assuming xml is in the string or this can come from file or any other stream; you can load it in XDocument and use linq to find your nodes and attributes.

    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
    }

Upvotes: 0

Tormod
Tormod

Reputation: 4573

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. Like this:

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

Upvotes: 2

Related Questions