获取上一个同级返回当前的同级

本文关键字:返回 获取 上一个 | 更新日期: 2023-09-27 17:56:47

我的目标是获取xml文件中的前一个元素并相应地显示它。不幸的是,当我使用以前的兄弟方法时,它会返回当前元素......

以前有人遇到过这个问题吗?如何解决这个问题?

应该注意,我的目标是按"num"属性进行搜索,以便在按唯一标识符而不是可以更改的值进行搜索方面遵守适当的编码标准。

我也知道我混淆了"xmlappChild.InnerText = txtAppName.txt"。

这是我的代码:

XML 文件格式:

<?xml version="1.0" encoding="utf-8"?>
<!--This file contains all the apps, versions, source and destination paths.-->
<applications>
<app num="1" desc="changed">
<appname>ARIA</appname>
<version>V 30</version>
<srcpath>c:'testing'</srcpath>
<dstpath>c:'testing'</dstpath>
</app>
<app num="2" desc="changed">
<appname>testing 123</appname>
<version>V 11</version>
<srcpath>C:'Users'Documents'testing'src</srcpath>
<dstpath>C:'Users'Documents'testing'dst</dstpath>
</app>
</applications>

C# 代码:

//loop through the xml to find what the user has selected in order to overwrite
            foreach (XmlNode xmlApp in xdoc.DocumentElement.ChildNodes)
            {
                //select the num attribute and if they match up...
                if (Convert.ToInt32(xmlApp.Attributes["num"].Value) == currappNum)
                {
                    //get the previous app node
                    xmlPrev = xmlApp.PreviousSibling; << this is where I get the problem
                    //replace the description attribute
                    xmlPrev.Attributes["desc"].Value = txtDesc.Text;
                    //loop though the child nodes in xml app 
                    foreach (XmlNode xmlAppChild in xmlPrev.ChildNodes)
                    {
                        //replace all the info 
                        switch (xmlAppChild.Name)
                        {
                            case "appname":
                                xmlAppChild.InnerText = txtAppName.Text;
                                break;
                            case "version":
                                xmlAppChild.InnerText = txtVersion.Text;
                                break;
                            case "srcpath":
                                xmlAppChild.InnerText = txtSrcPath.Text;
                                break;
                            case "dstpath":
                                xmlAppChild.InnerText = txtDstPath.Text;
                                break;
                            default:
                                break;
                        }
                    }
                    //set the current num
                    currappNum = Convert.ToInt32(xmlPrev.Attributes["num"].Value);
                }
               }
            }

获取上一个同级返回当前的同级

这段代码没有解释为什么你在使用PreviousSibling时遇到问题,但它确实实现了目标。在每次迭代结束时,将 xmlPrev 设置为 xmlApp。这意味着对于下一次迭代,xmlPrev 将等于上一个节点。

 var xmlPrev; // im assuming it is declared somewhere up here.      
foreach (XmlNode xmlApp in xdoc.DocumentElement.ChildNodes)
            {
            //select the num attribute and if they match up...
            if (Convert.ToInt32(xmlApp.Attributes["num"].Value) == currappNum)
            {
                //get the previous app node
                if (xmlPrev == null)
                {
                   //it must be the first element in the loop, so handle this however you need to.
                }
                //replace the description attribute
                xmlPrev.Attributes["desc"].Value = txtDesc.Text;
                //loop though the child nodes in xml app 
                foreach (XmlNode xmlAppChild in xmlPrev.ChildNodes)
                {
                    //replace all the info 
                    switch (xmlAppChild.Name)
                    {
                        case "appname":
                            xmlAppChild.InnerText = txtAppName.Text;
                            break;
                        case "version":
                            xmlAppChild.InnerText = txtVersion.Text;
                            break;
                        case "srcpath":
                            xmlAppChild.InnerText = txtSrcPath.Text;
                            break;
                        case "dstpath":
                            xmlAppChild.InnerText = txtDstPath.Text;
                            break;
                        default:
                            break;
                    }
                }
                //set the current num
                currappNum = Convert.ToInt32(xmlPrev.Attributes["num"].Value);
               }
                 xmlPrev = xmlApp; // Set the previous node here. Each time you loop through, the xmlPrev will be set to the previous element in the loop.
           }
        }