获取以前的兄弟姐妹返回当前的同级

时间:2014-04-04 17:22:00

标签: c# xml

我的目标是获取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);
                }
               }
            }

1 个答案:

答案 0 :(得分:0)

此代码无法解释您遇到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.

           }
        }