如何在C#中获取XML的所有子节点

时间:2015-11-18 09:01:03

标签: c# xml xmlnode xmlnodelist

我有一个这样的XML文档:

<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>

我正在获取他们的名称和宽度文本,并将它们存储为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);

所以,我只能得到这些值:A和100​​,C和300。 我也希望得到C1和310。我怎么能得到它们?

编辑:某些列没有列,某些列有1个或多个列。在此示例中,我的列表包含3个元素:

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

4 个答案:

答案 0 :(得分:1)

    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;
        }
    }

答案 1 :(得分:0)

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);
    }
}

}

答案 2 :(得分:0)

使用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();

答案 3 :(得分:0)

你可以这样做;如果需要,可以将其减少到2行。

                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;
        }