【问题标题】:Get Parent attribute values xml获取父属性值 xml
【发布时间】:2011-06-17 06:15:02
【问题描述】:

我有一个这样的 xml 文件


con1 连接> 目的地>


con2 连接> 目的地>

说明>


con3 连接> 目的地>


con4 连接> 目的地>

说明>

我必须得到所有的连接。我写的代码

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

我得到的输出是

con1
con2
con3
con4

根据我的新要求输出应该是

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

我是 .net 的新手,我使用的是 2.0 框架,我不能使用 LINQ。谢谢

【问题讨论】:

  • 你遇到了什么困难?
  • @Steve B 我试图像这个字符串指令 = xn["Instruction"].GetElementsByTagName("name").ToString();但它的抛出错误

标签: c# xml


【解决方案1】:

试试这样:

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

【讨论】:

    【解决方案2】:

    你可以这样写:

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

    但是,我认为这是一个非常简单的案例,您可以自己解决。这里没有技术挑战。我建议您参加培训,阅读一本书并深入研究文档。

    PS:不要使用StringBuilder代替字符串拼接...

    【讨论】:

    • 感谢您的回复。一个小的更正,在内部 foreach 循环中应该是 subNodes.InnerText 而不是 subNodes.Text
    【解决方案3】:

    类似这样的东西,带有一个内部循环:

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

    【讨论】:

      猜你喜欢
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      • 2016-08-01
      • 1970-01-01
      • 2011-11-06
      • 2016-11-05
      • 2021-11-17
      • 2011-07-26
      相关资源
      最近更新 更多