计算XML中的特定XML节点

时间:2010-02-18 09:05:44

标签: c# asp.net 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>

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

5 个答案:

答案 0 :(得分:12)

以下是计算xml文档中所有submenuid个节点而不将其加载到内存中的示例:

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

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

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

答案 1 :(得分:6)

像这样使用XmlDocument:

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

答案 2 :(得分:1)

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

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

答案 3 :(得分:0)

下面的代码是查找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()));
    }

答案 4 :(得分:0)

如果您可以控制xml

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $("#btn1").click(function() {
                $("p").append(" <b>Tekst you want to show after button click</b>.");
            });
        });
    </script>
</head>

<body>
    <p></p>
    <button id="btn1">Show tekst</button>
</body>
</html>