0

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.

2 Answers 2

2

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

could you please show how I now extract a value, for example the IPMode? As far as I can see this looks good!
Ah, neverminde, figured that out. string temp = element.Element("IPConfigurations").Attribute("IPmode").Value;
0

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
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.