Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 每个循环的Xml节点读取_C#_Xml_Loops_Foreach_Nodes - Fatal编程技术网

C# 每个循环的Xml节点读取

C# 每个循环的Xml节点读取,c#,xml,loops,foreach,nodes,C#,Xml,Loops,Foreach,Nodes,我试图循环遍历一个Xml文件,并在消息中显示account的值 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["valu

我试图循环遍历一个Xml文件,并在消息中显示account的值

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);
 }
消息框当前重复显示第一条记录,如何才能到达下一条记录


提前感谢您的输入。

您正在使用
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;
   ...

我不是100%确定,但您可能需要使用递归。如果不是,则应如下所示:

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

您不应该花时间逐节点读取xml。尝试:

尝试以下操作:

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

下面是获取子节点信息的父节点值示例。这里我使用ReportItems ParentNode并仅打印图像子节点

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

            }

        }
xmldoc.Load(rdlFile);
StringBuilder sb=新的StringBuilder();
XmlNode node=xmldoc.GetElementsByTagName(“ReportItems”)[0];
XmlNodeList=node.ChildNodes;
atributes=新字符串[node.ChildNodes.Count];
int l=0;
对于(int j=0;j
如果您有疑问,请告诉我。

试试这个

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

您没有使用no变量,即每次迭代的值将是一个起点-1:这完全取决于XML信息集的外观以及他试图如何处理它。反序列化到某个类型可能会在设计时和运行时进行大量不必要的工作,甚至可能根本不可能。@Chris~最佳实践总是比任何其他实践都好。只是想让OP知道反序列化确实存在。如果我问这个问题,我会提到我对反序列化不感兴趣,因为xml太复杂了,我只需要一小部分,他没有。我需要仔细阅读整篇文章。。。编辑以便我可以撤消-1。而且我仍然看不出OP为什么会使用递归——XPath已经选择了想要的深度的节点。。。