0

I posed a question last night regarding how to parse through repeating XML structures. The XML has two levels that repeat and the second level contains elements that I need to store elsewhere.

The question was marked as a duplicate and pointed to an accepted answer that stated 'use Linq to XML' basically.

While I appreciate the answer, the bottom of my question stated I've tried to understand how to use LINQ to XML, but couldn't figure out how to access the second level data.

Are there any easy to understand examples or tutorials on how to use LINQ to XML, or any other method for that matter, to take the information in an XML document and be able to loop through it?

I've done this in Perl previously and it basically sucks in the XML and creates a large object you can iterate through.

Link to original question: Parsing XML with C Sharp

Community
  • 1
  • 1
  • you can find a tutorial here : http://www.codeproject.com/Articles/24376/LINQ-to-XML – hdoghmen Jun 21 '15 at 16:15
  • 3
    Welcome to Stack Overflow. This question will probably get closed too because questions that ask for links to resources are discouraged (they get out of date too quickly). The best way to ask a question like this is to provide write the code of a simple example that does the parts you _can_ do and then ask about how to add the missing part. See http://stackoverflow.com/help/how-to-ask – Matthew Strawbridge Jun 21 '15 at 16:18

1 Answers1

0

They say "Practice make Perfect". Sometimes doing something a number of times is the best method for learning. Use the linq method "ToList()" like in the code below

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 =
                "<Data>" +
                  "<Report>" +
                    "<Machine name=\"hostA\">" +
                      "<MachineInfo location=\"LA\">" +
                        "<function name=\"run\">Late</function>" +
                        "<function name=\"status\">Complete</function>" +
                        "<function name=\"date\">2015-06-14</function>" +
                      "</MachineInfo>" +
                      "<RepItem name=\"1488\" direction=\"NS\">" +
                        "<Desc>None Found</Desc>" +
                        "<Status Int=\"A12\">Uplink</Status>" +
                      "</RepItem>" +
                      "<RepItem name=\"1489\" direction=\"S\">" +
                        "<Desc>31Ghz Ant at 285ft.</Desc>" +
                        "<Status Int=\"D5\">Active</Status>" +
                      "</RepItem>" +
                      "<RepItem name=\"1438\" direction=\"W\">" +
                        "<Desc>West N. Oc. Backup</Desc>" +
                        "<Status Int=\"A11\">Disabled</Status>" +
                      "</RepItem>" +
                      "<RepItem name=\"1141\" direction=\"SE\">" +
                        "<Desc>MDT Co.</Desc>" +
                        "<Status Int=\"B7\">Active</Status>" +
                      "</RepItem>" +
                    "</Machine>" +
                    "<Machine name=\"hostB\">" +
                      "<MachineInfo location=\"E. LA\">" +
                        "<function name=\"run\">Late</function>" +
                        "<function name=\"status\">Complete</function>" +
                        "<function name=\"date\">2015-06-14</function>" +
                      "</MachineInfo>" +
                      "<RepItem name=\"1488\" direction=\"NS\">" +
                        "<Desc>None Found</Desc>" +
                        "<Status Int=\"A12\">Down</Status>" +
                      "</RepItem>" +
                      "<RepItem name=\"1489\" direction=\"S\">" +
                        "<Desc>31Ghz Ant at 285ft.</Desc>" +
                        "<Status Int=\"D5\">Active</Status>" +
                      "</RepItem>" +
                      "<RepItem name=\"1438\" direction=\"W\">" +
                        "<Desc>West N. Oc. Backup</Desc>" +
                        "<Status Int=\"A11\">Disabled</Status>" +
                      "</RepItem>" +
                      "<RepItem name=\"1141\" direction=\"SE\">" +
                        "<Desc>MDT Co.</Desc>" +
                        "<Status Int=\"B7\">Active</Status>" +
                      "</RepItem>" +
                    "</Machine>" +
                  "</Report>" +
                "</Data>";

            XDocument doc = XDocument.Parse(input);
            var results = doc.Descendants("Machine")
                .Select(x => new {
                    name = x.Attribute("name").Value,
                    info = new {
                        machineInfo = x.Element("MachineInfo").Attribute("location").Value,
                        functions = x.Element("MachineInfo").Elements("function").Select(y => y.Value).ToList()
                    },
                    repItems = x.Elements("RepItem")
                        .Select(y => new {
                            name = y.Attribute("name").Value, 
                            direction = y.Attribute("direction").Value,
                            description = y.Element("Desc").Value,
                            status = y.Element("Status").Value,
                            index =  y.Element("Status").Attribute("Int").Value
                        }).ToList()
                })
                .ToList();
        }
    }
}
​
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • Descendant(s), Element(s), and Attribute(s) have singular and plural versions. When a tag appears multiple times then use the plural method followed by "Select" and then close with a List() method like in the code above. – jdweng Jun 21 '15 at 23:19