0

I have a simple xml file. Looks like this:

<?xml version="1.0" encoding="utf-8" ?> 
<activities>
  <task>
    <name> Task1 </name>
    <time> 00:00 </time>

    <subtask>
      <name> Task1 - subtask1 </name>
      <time> 00:00 </time>
    </subtask>

    <subtask>
      <name> Task1 - subtask2 </name>
      <time> 00:00 </time>
    </subtask>

  </task>

  <task>
    <name> Task2 </name>
    <time> 00:00 </time>

    <subtask>
      <name> Task2 - subtask1 </name>
      <time> 00:00 </time>
    </subtask>

  </task>

</activities>

My question is how could I get each subtask nodes from each tasks? For example first of all I want to get Task 1 and only the relevant subtasks (subtask1, subtask2) and their data.

How could I do that in C# in the appropriate way? I'm new to XML. :)

erbal
  • 727
  • 3
  • 12
  • 32
  • 1
    Have you tried anything? – EkoostikMartin Jul 17 '15 at 15:17
  • 1
    Possible duplicate of [How to read XML in .net](http://stackoverflow.com/questions/4752796/how-to-read-xml-in-net) OR [C# - Reading data from XML](http://stackoverflow.com/questions/7119806/c-sharp-reading-data-from-xml) – gastonmancini Jul 17 '15 at 15:20
  • Agree with @EkoostikMartin, please do some research on XPATH queries, see how to use XPATH in your C# code, and then if you encounter issues, post the question with the details of what you tried. Don't expect others to do your homework for you. – Scorpion-Prince Jul 17 '15 at 15:26
  • I've just figure it out. :) The node is a task node. XmlNodeList subtasks = node.SelectNodes("subtask"); – erbal Jul 17 '15 at 15:34

3 Answers3

0

Use XPath syntax to select what you need.

Use the XmlDocument class in .Net along with your XPath expression.

citywall
  • 235
  • 2
  • 11
0

Try the code below:

  String myXML = @" <?xml version='1.0' encoding='utf-8' ?> 
            <activities>
              <task>
                <name> Task1 </name>
                <time> 00:00 </time>

                <subtask>
                  <name> Task1 - subtask1 </name>
                  <time> 00:00 </time>
                </subtask>

                <subtask>
                  <name> Task1 - subtask2 </name>
                  <time> 00:00 </time>
                </subtask>

              </task>

              <task>
                <name> Task2 </name>
                <time> 00:00 </time>

                <subtask>
                  <name> Task2 - subtask1 </name>
                  <time> 00:00 </time>
                </subtask>

              </task>

            </activities>";
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(myXML);
        XmlNodeList items = doc.DocumentElement.SelectNodes("//subtask");

The SelectNodes parameter is an XPath query that select all subtasks which are direct children of task. You can find more information about XPath here: http://www.w3schools.com/xpath/default.asp

You can read the information as below:

        List<String> names=new List<string>();
        for(int i=0;i<items.Count;i++)
        {
            names.Add(items.Item(i).InnerText);
        }
Nick Mehrdad Babaki
  • 11,560
  • 15
  • 45
  • 70
0

Try XML LInq

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = 
              "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + 
                "<activities>" +
                  "<task>" +
                    "<name>Task1</name>" +
                    "<time>00:00</time>" +

                    "<subtask>" +
                      "<name>Task1 - subtask1</name>" +
                      "<time>00:00</time>" +
                    "</subtask>" +

                    "<subtask>" +
                      "<name>Task1 - subtask2</name>" +
                      "<time>00:00</time>" +
                    "</subtask>" +

                  "</task>" +

                  "<task>" +
                    "<name>Task2</name>" +
                    "<time>00:00</time>" +

                    "<subtask>" +
                      "<name>Task2 - subtask1</name>" +
                      "<time>00:00</time>" +
                    "</subtask>" +

                  "</task>" +

                "</activities>" ;

            XDocument doc = XDocument.Parse(input);
            var results = doc.Descendants("task").Select(x => new {
                name = x.Element("name").Value,
                time = x.Element("time").Value,
                subtask = x.Elements("subtask").Select(y => new {
                    name = y.Element("name").Value,
                    time = y.Element("time").Value
                }).ToList()
            }).ToList();

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