每个循环的 Xml 节点读取

发布于 2024-10-11 03:55:49 字数 383 浏览 7 评论 0原文

我正在尝试循环遍历 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);
 }

消息框当前重复显示第一条记录,如何才能转到下一条记录?

感谢您提前提供意见。

I am trying to loop through an Xml file and display the value for account in a message.

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

The message box is currently displaying the first record repeatidly, how can I get to the next record?

Thanks for your input in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

海未深 2024-10-18 03:55:49

您重复使用 testDoc 中的相同元素分配 node。不清楚 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;
   ...

You are repeatedly assigning node with the same element from testDoc. It is not clear what test.account is (perhaps a mistype for test.actual)?

no is the variable which will iterate the contents of nodeList - I imagine you intended to use that.

EDIT following edit of OP
Now you've shown us what nodeList is, I suspect you want to do something like this instead :

XmlNodeList nodeList = testDoc.SelectNodes("/details/row/var[@name='account']"); 
foreach (XmlNode no in nodeList) 
{    
   test.actual = no.Attributes["value"].Value;
   ...
罗罗贝儿 2024-10-18 03:55:49
        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 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);
            }
        } 
金兰素衣 2024-10-18 03:55:49

这是父节点值的示例,用于获取子节点的信息。这里我使用 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");
                }

            }

        }

如果您有疑问,请告诉我..

Here is the sample for parent node value to get information of the child nodes.here i am using the ReportItems ParentNode and Print only image child nodes.

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

            }

        }

let me know if you have doubt..

吹泡泡o 2024-10-18 03:55:49

试试这个,

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

Try this,

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);
}
我乃一代侩神 2024-10-18 03:55:49

请尝试以下操作:

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

Try the following:

        //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());
        }
静谧 2024-10-18 03:55:49

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

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

I'm not 100% sure, but you may need to use recursion. If not, it should just look like this:

XmlDocument doc = //etc..
foreach(XmlNode node in doc.ChildNodes)
{
    if(node.Name == "account")
    {
        MessageBox.Show(node.Value);
    }
}
相对绾红妆 2024-10-18 03:55:49

您不应该花时间逐个节点地读取 xml。尝试反序列化

You shouldn't spend time with reading the xml node by node. Try Deserialization:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文