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

1teamsah picture 1teamsah · Nov 18, 2015 · Viewed 22.3k times · Source

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}

Answer

Karthikeyan picture Karthikeyan · Nov 18, 2015
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 (root.NextSibling != null)
            RecurseXmlDocument(root.NextSibling);
    }
}

}