0

I have this XML file:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfPasswordSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <PasswordSettings>
        <CustomerRef>c</CustomerRef>
        <Node>n</Node>
        <Name>na</Name>
        <Login>l</Login>
        <Password>ITra+Map1RxklmcSY5yOo9wU9tUV0S4C4qwUv4p2ZFS3L8ByJYXmA9YjswlSTjQZXUJAkV3Z6mhY8OF5/dFOLNAZZRk2i2IOzrVOWSDfdpB8/Vm7PPF0ucSHILHNWpT8</Password>
        <FileType>ft</FileType>
    </PasswordSettings>

    <PasswordSettings>
        <CustomerRef>c</CustomerRef>
        <Node>n</Node>
        <Name>na</Name>
        <Login>l</Login>
        <Password>ITra+Map1RxklmcSY5yOo9wU9tUV0S4C4qwUv4p2ZFS3L8ByJYXmA9YjswlSTjQZXUJAkV3Z6mhY8OF5/dFOLNAZZRk2i2IOzrVOWSDfdpB8/Vm7PPF0ucSHILHNWpT8</Password>
        <FileType>ft</FileType>
    </PasswordSettings>

</ArrayOfPasswordSettings>

As you see there are multiple <PasswordSettings> which is a list of multiple items like name, login and password. Can I iterate the <PasswordSettings> in some foreach <PasswordSettings> loop and get the elements?

  • You will hvae to read the file as XML, there are then many possibilities. Please check [Reading Data from XML](https://stackoverflow.com/questions/7119806/reading-data-from-xml) – Malior Apr 10 '19 at 08:10
  • 2
    Possible duplicate of [How to Deserialize XML document](https://stackoverflow.com/questions/364253/how-to-deserialize-xml-document) – Sani Huttunen Apr 10 '19 at 08:10
  • Later on, you should really try Xml Linq. It exists in C#, I've tried it and it's really good – Mohamad Mousheimish Apr 10 '19 at 08:39

2 Answers2

0

Please try with this example :
XML:

 <?xml version="1.0" encoding="utf-8" standalone="yes"?>
<root>
    <Brand name="Brand1">
        <product name="Product1" />
        <product name="Product2" />
    </Brand>
    <Brand name="Brand2">
        <product name="Product3" />
        <product name="Product4" />
    </Brand>
</root>

C#:

                StringBuilder result = new StringBuilder();
                foreach (XElement level1Element in XElement.Load(@"D:\product.xml").Elements("Brand"))
                {
                    result.AppendLine(level1Element.Attribute("name").Value);
                    foreach (XElement level2Element in level1Element.Elements("product"))
                    {
                        result.AppendLine("  " + level2Element.Attribute("name").Value);
                    }
                }
0

Try xml linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication108
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var results = doc.Descendants("PasswordSettings").Select(x => new
            {
                c = (string)x.Element("CustomerRef"),
                node = (string)x.Element("Node"),
                name = (string)x.Element("Name"),
                login = (string)x.Element("Login"),
                password = (string)x.Element("Password"),
                fileType = (string)x.Element("FileType")
            }).ToList();

        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20