C# 计算 XML 中的特定 XML 节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2287384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:05:13  来源:igfitidea点击:

Count specific XML Nodes within XML

c#asp.netxml

提问by Jobi

See this XML:

请参阅此 XML:



<CMP>
    <OMP3>
        <personmenu>
            <submenuid>502</submenuid>
            <submenuid>503</submenuid>
        </personmenu>
        <accountsmenu>
            <submenuid>517</submenuid>
            <submenuid>518</submenuid>
            <submenuid>519</submenuid>
        </accountsmenu>

        <reportsmenu>
            <submenuid>522</submenuid>
            <submenuid>528</submenuid>
            <submenuid>536</submenuid>
        </reportsmenu>
    </OMP3>

    <AMP3>
        <admissionmenu>
            <submenuid>702</submenuid>
            <submenuid>703</submenuid>
        </admissionmenu>
    </AMP3>
</CMP>


I want to get the total count of nodes from this xml dynamically (C#). How can I do it? Any sample code?

我想从这个 xml 动态获取节点总数(C#)。我该怎么做?任何示例代码?

回答by sashaeve

Use XmlDocument like this:

像这样使用 XmlDocument:

XmlDocument xmlD = new XmlDocument();
xmlD.Load(Server.MapPath("sample.xml"));
XmlNodeList xmlNL = xmlD.GetElementsByTagName("tagName");
xmlNL.Count;

回答by Darin Dimitrov

Here's an example of counting all submenuidnodes in your xml document without loading it into memory:

这是一个计算submenuidxml 文档中所有节点而不将其加载到内存中的示例:

var nodeCount = 0;
using (var reader = XmlReader.Create("test.xml"))
{
    while (reader.Read())
    {
        if (reader.NodeType == XmlNodeType.Element && 
            reader.Name == "submenuid")
        {
            nodeCount++;
        }
    }
}
Console.WriteLine(nodeCount);

Or if you prefer LINQ to XML:

或者,如果您更喜欢 LINQ to XML:

var count = XDocument
    .Load("test.xml")
    .XPathSelectElements("//submenuid")
    .Count();

回答by particle

You can use XPath function "count" as well. here is a example.

您也可以使用 XPath 函数“count”。这是一个例子。

XPathDocument doc = new XPathDocument("c:\test.xml");
int count = (int)doc.CreateNavigator().Evaluate("count(//submenuid)");

回答by saranyathirumalai

Below code is to find the count of a specific node in the XML Document

下面的代码是查找特定节点的计数 XML Document

 private void browse_Click(object sender, EventArgs e)//file browse button
    {
        DialogResult result = openFileDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            String file = openFileDialog1.FileName;
            if (Path.GetExtension(file) != ".xml")
            {
                MessageBox.Show("Please upload an vaild xml file");
                textBox1.Clear();
            }
            else
            {
                textBox1.Text = file;
            }
        }
    }  
 private void CountButton_Click(object sender, EventArgs e)//count button
    {
        int count = 0;
        string element = textBox2.Text;//Enter the node in the textbox
        XmlDocument readdoc = new XmlDocument();
            readdoc.Load(textBox1.Text);
            XmlElement root = readdoc.DocumentElement;
            XmlNodeList node = root.GetElementsByTagName(element);
            count = node.Count;
            MessageBox.Show(string.Format("Count of {0} node in the uploaded xml file is {1}", element, count.ToString()));
    }

回答by Jonathan Burgos Gio

If you have control of your xml

如果您可以控制您的 xml

string xmlReturn="
<CMP>
<OMP3>
    <personmenu>
        <submenuid>502</submenuid>
        <submenuid>503</submenuid>
    </personmenu>
    <accountsmenu>
        <submenuid>517</submenuid>
        <submenuid>518</submenuid>
        <submenuid>519</submenuid>
    </accountsmenu>

    <reportsmenu>
        <submenuid>522</submenuid>
        <submenuid>528</submenuid>
        <submenuid>536</submenuid>
    </reportsmenu>
</OMP3>

<AMP3>
    <admissionmenu>
        <submenuid>702</submenuid>
        <submenuid>703</submenuid>
    </admissionmenu>
</AMP3>
</CMP>"
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(xmlReturn);
XmlNodeList nodeListCount=xmldoc.GetElementsByTagName("submenuid");
int nodeCount = nodeListCount.Count;
Console.WriteLine(nodeCount);