簡體   English   中英

如何在C#中獲取XML的所有子節點

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

我有一個像這樣的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個或多個列。 在此示例中,我的List包含3個元素:

lst[0][0] = {A, 100}
lst[1][0] = null
lst[2][0] = {C, 300}, lst[2][1] = {C1, 310}
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);
    }
}

}

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

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

你可以這樣做; 如果需要,可以將其減少到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;
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM