逐个循环读取XML节点

10
我试图循环遍历一个XML文件并在消息中显示账户的值。
XmlNodeList nodeList = testDoc.SelectNodes("/details/row/var");
foreach (XmlNode no in nodeList)
{
   XmlNode node = testDoc.SelectSingleNode("/details/row/var[@name='account']");
   test.actual = node.Attributes["value"].Value;

   MessageBox.Show(test.account);
 }

消息框当前重复显示第一条记录,如何获取下一条记录?

提前感谢您的回答。


2
你没有使用 no 变量,它是每次迭代的起点值。 - Aaron McIver
7个回答

15

你正在反复使用node,并将其赋值为来自testDoc的相同元素。不清楚test.account是什么(也许是test.actual的打字错误)?

no是变量,将迭代nodeList的内容 - 我想你本意是要使用它。

编辑 根据OP的编辑 现在你已经展示了nodeList的内容,我怀疑你想要做的是这样的:

XmlNodeList nodeList = testDoc.SelectNodes("/details/row/var[@name='account']"); 
foreach (XmlNode no in nodeList) 
{    
   test.actual = no.Attributes["value"].Value;
   ...

5
        XmlDocument doc = new XmlDocument();
        doc.Load("d:\\test.xml");
        XmlNodeList node = doc.GetElementsByTagName("w:r");
        foreach (XmlNode xn in node)
        {
            try
            {
                if (xn["w:t"].InnerText != null)
                {
                    if (xn["w:t"].InnerText == "#")
                    {
                        string placeHolder = xn["w:t"].InnerText;
                        foreach (XmlNode a in node)
                        { 
                            if (a["w:t"].InnerText != "#")
                            {
                                string placeHolder1 = a["w:t"].InnerText;
                            }
                        } 
                    }
                }
            }

            catch (Exception e)
            {
                Console.Write(e);
            }
        } 

3

这是一个示例,用于获取子节点信息的父节点值。在这里,我使用ReportItemsParentNode,并仅打印图像子节点。

        xmldoc.Load(rdlFile);
        StringBuilder sb=new StringBuilder();
        XmlNode node = xmldoc.GetElementsByTagName("ReportItems")[0];
        XmlNodeList list = node.ChildNodes;
        atributes=new string[node.ChildNodes.Count];
        int  l = 0;
        for (int j = 0; j < node.ChildNodes.Count; j++)
        {


            if (list[j].Name == "Image")
            {
                XmlAttributeCollection att = list[j].Attributes;
                atributes[l] = att[0].Value.ToUpper();

            }
            l++;
        }
        for (int i = 0; i < node.ChildNodes.Count; i++)
        {
            if (searchText.Text.ToUpper() == atributes[i])
            {
                XmlNodeList lastlist = node.ChildNodes;
                XmlNodeList endlist = lastlist[i].ChildNodes;
                for (int k = 0; k < endlist.Count; k++)
                {
                    sb.Append(endlist[k].Name+" - "+ endlist[k].InnerText);
                    sb.Append("\n"+"\n");
                }

            }

        }

如果你有疑问,请告诉我。


3
尝试这个:

XmlDocument xdoc = new XDocument();

xdoc.Load("*/File/*"); 
string xmlcontents = xdoc.InnerXml;

var xpath = "(/details/row/var[@name='account'])";

XmlNodeList lists = xdoc.DocumentElement.SelectNodes(xpath);

foreach (XmlNode _node in lists)
{
    string _nodeValue = _node.InnerText;
    MessageBox.Show(_nodeValue);
}

1

请尝试以下方法:

        //Create an xml reader;
        XmlDocument _xmlDocument = new XmlDocument();
        _xmlDocument.Load(/*File Name here*/);

        //Select the element with in the xml you wish to extract;
        XmlNodeList _nodeList = _xmlDocument.SelectNodes("/details/row/var[@name='account']");

        //Display the values in the node list to the screen;
        foreach (XmlNode _node in _nodeList)
        {
            String _nodeValue = _node.InnerText.ToString();
            MessageBox.Show(_nodeValue.ToString());
        }

0

我不是100%确定,但你可能需要使用递归。如果不需要,它应该看起来像这样:

XmlDocument doc = //etc..
foreach(XmlNode node in doc.ChildNodes)
{
    if(node.Name == "account")
    {
        MessageBox.Show(node.Value);
    }
}

我需要仔细阅读整篇文章...编辑以便我可以撤销-1。此外,我仍然看不出为什么OP要使用递归 - XPath已经选择了深度为所需的节点... - Alexei Levenkov

0

你不应该花费时间逐个节点读取xml。尝试使用反序列化


-1:这完全取决于XML信息集的样子以及他想用它做什么。在设计时间和运行时反序列化到一个类型可能会是大量不必要的工作,或者甚至不可能实现。 - Chris Dickson
@Chris ~ 最佳实践总是比其他任何实践更好。只是想让OP知道反序列化确实存在。如果我在问这个问题,我会提到我不感兴趣反序列化,因为XML太复杂了,而我只需要他没有的一小部分。 - Pabuc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接