JonasAnon
JonasAnon

Reputation: 195

convert data of XML file to string lists C#

This is how the XML file could look:

<data>
  <subdata>
    <datatype id="1" name="data1">
    <xdim>2</xdim>
    <ydim>1</ydim>
    </datatype>
    <datatype id="2" name="data2">
    <xdim>3</xdim>
    <ydim>4</ydim>
    </datatype>
  </subdata>
</data>

Now, i want the following:

Are there easy methods built in in C# for stuff like this? Or could anyone help me with this question?

Upvotes: 1

Views: 1450

Answers (6)

Viru
Viru

Reputation: 2246

You can use Descendents method.
This method reads all the child nodes even the nested ones where node name matches with specified string.

  var Idstring = MyXml.Descendants("datatype").Select (x=>x.Attribute("Id")).ToList();

    var xdimstring = MyXml.Descendants("xdim").Select (x=>x.Value).ToList();

    var ydimstring = MyXml.Descendants("ydim").Select (x=>x.Value).ToList();

To Appease your curiosity :) This how you can get nodes from specifically subdata node.

var Idstring = MyXml.Descendants("Subdata").Descendants("datatype").Select (x=>x.Attribute("Id")).ToList();

    var xdimstring = MyXml.Descendants("Subdata").Descendants("xdim").Select (x=>x.Value).ToList();

    var ydimstring = MyXml.Descendants("Subdata").Descendants("ydim").Select (x=>x.Value).ToList();

Now let say you have Multiple subdata and you want to read nodes only from first one...Simply use First linq extension method

var Idstring = MyXml.Descendants("Subdata").First().Descendants("datatype").Select (x=>x.Attribute("Id")).ToList();

Upvotes: 5

jdweng
jdweng

Reputation: 34431

Try something like this

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

namespace ConsoleApplication81
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
                "<data>" +
                  "<subdata>" +
                    "<datatype id=\"1\" name=\"data1\">" +
                    "<xdim>2</xdim>" +
                    "<ydim>1</ydim>" +
                    "</datatype>" +
                    "<datatype id=\"2\" name=\"data2\">" +
                    "<xdim>3</xdim>" +
                    "<ydim>4</ydim>" +
                    "</datatype>" +
                  "</subdata>" +
                "</data>";

            XElement data = XElement.Parse(xml);
            var results = data.Descendants("subdata").Elements()
                .GroupBy(x => x.Name.LocalName)
                .Select(x => new
                {
                    name = x.Key,
                    value = x.Select(y => (string)y).ToList(),
                    attributes = x.Attributes()
                       .Select(y => new {name = y.Name.LocalName, y.Value})
                       .GroupBy(y => y.name, z => z.Value)
                       .ToDictionary(y => y.Key, z => z.ToList())
                }).ToList();

        }
    }
}

Upvotes: 0

silver
silver

Reputation: 1703

        var xmlDocument = new XmlDocument();
        xmlDocument.LoadXml(
            "<data><subdata><datatype id=\"1\" name=\"data1\"><xdim>2</xdim><ydim>1</ydim></datatype><datatype id=\"2\" name=\"data2\"><xdim>3</xdim><ydim>4</ydim></datatype></subdata></data>");
        var nodes = xmlDocument.SelectNodes("//datatype");
        var first = new List<string>();
        var Second = new List<string>();
        var third = new List<string>();
        foreach (XmlNode node in nodes)
        {
            first.Add(node.Attributes["id"].Value);
        }
        nodes = xmlDocument.SelectNodes("//xdim");
        foreach (XmlNode node in nodes)
        {
            Second.Add(node.InnerText);
        }
        nodes = xmlDocument.SelectNodes("//ydim");
        foreach (XmlNode node in nodes)
        {
            third.Add(node.InnerText);
        }

Upvotes: 1

Giorgos Betsos
Giorgos Betsos

Reputation: 72205

You can use:

XDocument doc = XDocument.Load(@"..\myfile.xml");

to load your file in a XDocument object.

Then use XDocument methods to create string lists of the required id values:

var ids = (from a in doc.Descendants("subdata").Elements().Attributes("id")
           select a.Value).ToList();

var xids = (from e in doc.Descendants("datatype").Elements("xdim")
            select e.Value).ToList();

var yids = (from e in doc.Descendants("datatype").Elements("ydim")
            select e.Value).ToList();

Upvotes: 1

Richard Irons
Richard Irons

Reputation: 1473

This works and is fairly neat and simple:

string xml = 
@"<data>
<subdata>
    <datatype id=""1"" name=""data1"">
    <xdim>2</xdim>
    <ydim>1</ydim>
    </datatype>
    <datatype id=""2"" name=""data2"">
    <xdim>3</xdim>
    <ydim>4</ydim>
    </datatype>
</subdata>
</data>";

var xelem = XElement.Parse(xml);

var allIDs = xelem
    .Descendants()
    .Where (x => x.Attribute("id") != null)
    .Select (x => x.Attribute("id").Value)
    .ToList();

var allXdims = xelem
    .XPathSelectElements("//xdim")
    .Select (x => x.Value)
    .ToList();

var allYdims = xelem
    .XPathSelectElements("//ydim")
    .Select (x => x.Value)
    .ToList();

Obviously the part at the start is just getting the XML into an XElement. You might want to do this with:

var xelem = XElement.Load(myXmlLocation);

instead.

Upvotes: 4

NickT
NickT

Reputation: 214

The easiest way to convert classes to and from XML in C# is XML Serialization

For your example, you could create a class with member variables corresponding to the tags in your XML. When you want to create your XML file, you serialize the class to an XML File.

When your want to read back the information, you deserialize the contents of the XML file back to the class you created.

Here's a more comprehensive article: https://msdn.microsoft.com/en-us/library/58a18dwa(v=vs.110).aspx

Upvotes: 3

Related Questions