简体   繁体   English

获取父级属性值xml

[英]Get Parent attribute values xml

I have a xml file like this 我有一个这样的xml文件

< insrtuction name=inst1> <安装名称= inst1>

< destnation > <目标>
< connection> con1 < /connection> < /destination> <连接> con1 </连接> </目的地>

< destnation > <目标>
< connection> con2 < /connection> < /destination> <连接> con2 </连接> </目的地>

< /instruction> </说明>

< insrtuction name=inst2> <指示名称= inst2>

< destnation > <目标>
< connection> con3 < /connection> < /destination> <连接> con3 </连接> </目的地>

< destnation > <目标>
< connection> con4 < /connection> < /destination> <连接> con4 </连接> </目的地>

< /instruction> </说明>

I have to get all the connections. 我必须获得所有联系。 code I wrote 我写的代码

    private void button5_Click(object sender, EventArgs e)
    {
        xml = new XmlDocument();
        xml.Load("D:\\connections.xml");
        string text = "";
        XmlNodeList xnList = xml.SelectNodes("/instruction/destination");
        foreach (XmlNode xn in xnList)
        {
            string configuration = xn["connection"].InnerText;               
            text = text + configuration + "\r\n" + "\r\n";
        }
        textBox1.Text=text;
    }        

Output I am getting is 我得到的输出是

con1
con2
con3
con4

According to my new requirement output should be 根据我的新要求,输出应为

Instruction Name : inst1
connection: con1
connection: con1
Instruction Name : inst2
connection: con3
connection: con4

I am new to .net, I am using 2.0 frame work, I cant use LINQ. 我是.net的新手,正在使用2.0框架,不能使用LINQ。 Thanks 谢谢

try like this: 尝试这样:

    xml = new XmlDocument();
    xml.Load("D:\\connections.xml");
    string val="";
    string text = "";
    foreach (XmlNode child in xml.DocumentElement.ChildNodes)
        {
            if (child.NodeType == XmlNodeType.Element)
            {
                //MessageBox.Show(child.Name + ": " + child.InnerText.ToString());
                node = child.Name; //here you will get node name
                if (node.Equals("Instruction"))
                {
                    val = child.InnerText.ToString(); //value of the node
                    //MessageBox.Show(node + ": " + val);
                }
            }
        }

Something like this, with an inner loop: 像这样,带有一个内循环:

        XmlNodeList xnList = xml.SelectNodes("/instruction");
        foreach (XmlElement xn in xnList)
        {
            text += "Instruction Name : " + xn.GetAttribute("name") + Environment.NewLine + Environment.NewLine;
            foreach (XmlElement cn in xn.SelectNodes("connection"))
            {
                text += "Connection : " + xn.InnerText + Environment.NewLine + Environment.NewLine;
            }
        }

You may write this : 您可以这样写:

private void button5_Click(object sender, EventArgs e)
{
    xml = new XmlDocument();
    xml.Load("D:\\connections.xml");
    StringBuilder sb = new StringBuilder();
    XmlNodeList xnList = xml.SelectNodes("/instruction");
    foreach (XmlNode xn in xnList)
    {
        sb.AppendLine(xn.Attribute["name"].Value);
        foreach(XmlNode subNodes in xn.SelectNodes("destination/connection") {
            sb.AppendLine(subNodes.InnerText);
        }
    }
    textBox1.Text=sb.ToString();
}     

However, I think this a very easy case you could have solved yourself. 但是,我认为这是一个非常简单的案例,您可以解决自己的问题。 There is no technical challenge here. 这里没有技术挑战。 I advise you to take a training, read a book and dive into the documentation. 我建议您接受培训,看书并深入阅读文档。

PS: not the use of StringBuilder instead of string concatenation... PS:不使用StringBuilder代替字符串串联...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM