Pratik
Pratik

Reputation: 1512

How to read XML String into List or any collection

I have string simillar to

xmlDataEntAddAttr = @"<ADDITIONAL INFO>
                            <Host> Test Host Name </Host>
                            <IP Address>Test IP Address</IP Address>
                            <Domain>Test Domain</Domain>
                            <Host Method>Test Host Method</Host Method>
                            <Flag>Test Flag</Flag>
                      </ADDITIONAL INFO>";

How can i read this into a String list with tag names and Values in it or in any collection. Please not the inner Tags like <Host></Host> can be any value only constant thing here is the root means <ADDITIONAL INFO></ADDITIONAL INFO>.

Please suggest any method to parse.

Upvotes: 1

Views: 4926

Answers (3)

TYY
TYY

Reputation: 2716

Note you need to remove the spaces in Additional Info, IP Address and Host Method

        var xmlDataEntAddAttr = @"<?xml version=""1.0""?>
                        <ADDITIONALINFO>
                        <Host> Test Host Name </Host>
                        <IPAddress>Test IP Address</IPAddress>
                        <Domain>Test Domain</Domain>
                        <HostMethod>Test Host Method</HostMethod>
                        <Flag>Test Flag</Flag>
                  </ADDITIONALINFO>";
        var xDocument = XDocument.Parse(xmlDataEntAddAttr);
        if (xDocument.Root != null)
        {
            var coll = xDocument.Root.Elements().Select(x => new {Name = x.Name, Value = x.Value});
            foreach (var item in coll)
            {
              Console.WriteLine("Name: {0}\tValue: {1}", item.Name, item.Value);
            }
        }
        Console.ReadLine();

Upvotes: 1

C&#233;dric Bignon
C&#233;dric Bignon

Reputation: 13022

Use a XDocument (in the System.Xml.Linq namespace) and Linq to Xml.

XDocument xDocument = XDocument.Parse(xmlDataEntAddAttr);
Dictionary<string, string> result = xDocument.Root.Elements().ToDictionary(element => element.Name.LocalName, element => element.Value);

However, your string is not a valid XML. You can't have whitespace in the XML element names. A valid version of you string is:

xmlDataEntAddAttr = @"<ADDITIONAL_INFO>
                            <Host> Test Host Name </Host>
                            <IP_Address>Test IP Address</IP_Address>
                            <Domain>Test Domain</Domain>
                            <Host_Method>Test Host Method</Host_Method>
                            <Flag>Test Flag</Flag>
                      </ADDITIONAL_INFO>";

Upvotes: 3

will simmons
will simmons

Reputation: 187

If my xml is structured like the following:

<nodes>
    <node id="123">
        <text>text goes here</text>
    </node>
</nodes>

Then I could do this:

XDocument xDoc;
using(StringReader sr = new StringReader("thexml"))
{
    xDoc = XDocument.Load(sr);
}
myDictionary = xDoc.Descendants("node").ToDictionary(x => x.Attribute("id").Value, x =>         x.Descendants("text").First().Value);

Upvotes: 1

Related Questions