1teamsah
1teamsah

Reputation: 1933

How to get all child nodes of an XML in C#

I've an XML document like this:

<Columns>
  <Column>
    <Name>A</Name>
    <Width>100</Width>
  </Column>
</Columns>

<Columns>

</Columns>

<Columns>
  <Column>
    <Name>C</Name>
    <Width>300</Width>
  </Column>
  <Column>
    <Name>C1</Name>
    <Width>310</Width>
  </Column>
</Columns>

I'm getting their Name and Width text and store them a List.

var man = new XmlNamespaceManager(xdoc.NameTable);
man.AddNamespace("ns", "http://schemas.microsoft.com/project");
List <string> lstText = new List<string>();
List <List<string>> lst = new List<List<string>>();
XmlNodeList xnList = xdoc.SelectNodes("/ns:Columns/ns:Column", man);

foreach (XmlNode xn in xnList)
        {
           lstText.Add(xn["Name"].InnerText));
           lstText.Add(xn["Width"].InnerText));
        }
lst.Add(lstText);

So, I can only get these values: A and 100, C and 300. I want to get C1 and 310 too. How can I get them?

Edit: Some of Columns has no Column, some of Columns has 1 or more Colums. In this sample, my List has 3 elements:

lst[0][0] = {A, 100}
lst[1][0] = null
lst[2][0] = {C, 300}, lst[2][1] = {C1, 310}

Upvotes: 6

Views: 35243

Answers (4)

Karthikeyan
Karthikeyan

Reputation: 373

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

public class MainClass
{
    public static void Main()
    {
        XmlDocument mDocument = new XmlDocument();
        XmlNode mCurrentNode;

        mDocument.Load("XPathQuery.xml");
        mCurrentNode = mDocument.DocumentElement;


        XmlNodeList nodeList = mCurrentNode.SelectNodes("*");
        DisplayList(nodeList);
    }

    static void DisplayList(XmlNodeList nodeList)
    {
        foreach (XmlNode node in nodeList)
        {
            RecurseXmlDocumentNoSiblings(node);
        }
    }

    static void RecurseXmlDocumentNoSiblings(XmlNode root)
    {
        if (root is XmlElement)
        {
            Console.WriteLine(root.Name);
            if (root.HasChildNodes)
                RecurseXmlDocument(root.FirstChild);
        }
        else if (root is XmlText)
        {
            string text = ((XmlText)root).Value;
            Console.WriteLine(text);
        }
        else if (root is XmlComment)
        {
            string text = root.Value;
            Console.WriteLine(text);
            if (root.HasChildNodes)
                RecurseXmlDocument(root.FirstChild);
        }
    }

    static void RecurseXmlDocument(XmlNode root)
    {
        if (root is XmlElement)
        {
            Console.WriteLine(root.Name);
            if (root.HasChildNodes)
                RecurseXmlDocument(root.FirstChild);
            if (root.NextSibling != null)
                RecurseXmlDocument(root.NextSibling);
        }
        else if (root is XmlText)
        {
            string text = ((XmlText)root).Value;
            Console.WriteLine(text);
        }
        else if (root is XmlComment)
        {
            string text = root.Value;
            Console.WriteLine(text);
            if (root.HasChildNodes)
                RecurseXmlDocument(root.FirstChild);
            if (vroot.NextSibling != null)
                RecurseXmlDocument(root.NextSibling);
        }
    }
}

Upvotes: 4

Carlo V. Dango
Carlo V. Dango

Reputation: 13902

    public readonly Dictionary<string, int> XmlValues = new Dictionary<string, int>();
    public void Analyze(XmlDocument xml)
    {
        RecurseXmlDocument(xml.LastChild);
    }

    void RecurseXmlDocument(XmlNode root)
    {
        switch (root.NodeType)
        {
            case XmlNodeType.Element:
                if (root.HasChildNodes)
                    RecurseXmlDocument(root.FirstChild);
                if (root.NextSibling != null)
                    RecurseXmlDocument(root.NextSibling);
                break;

            case XmlNodeType.Text:
                DictionayHelper.AddValue(XmlValues, root.Value);
                break;
        }
    }

Upvotes: 1

Kosala W
Kosala W

Reputation: 2143

You can do it this way; If you need you can reduce it down to 2 lines.

                var xDoc = XDocument.Load(@"c:\XPathQuery.xml");
        //this query gets Names and widths
        var widths = xDoc.Descendants().Where(x => x.Name.LocalName.Equals("Name")).Select(x => new { Name = x.Value, Width = x.ElementsAfterSelf().First().Value }).ToList();

        //if you want to loop through the collection, you can do like this.
        foreach (var width in widths)
        {
            var name = width.Name;
            var value = width.Width;
        }

Upvotes: 0

reckface
reckface

Reputation: 5878

Use System.Xml.Linq:

const string name = "Name";
var xml = XDocument.Load(@"P:\athToYour\columns.xml");
var columns = xml.Descendants(name).Select(x => new
{
    Name = x.Value,
    Width = x.ElementsAfterSelf("Width").FirstOrDefault().Value
})
.ToDictionary(x=> x.Name, x=> x.Width);
columns.Dump();

Upvotes: 0

Related Questions