【问题标题】:Can't get attribute name of xml element无法获取 xml 元素的属性名称
【发布时间】:2014-01-22 18:50:08
【问题描述】:

你好,我得到了这个 xml 文件

坦尼娅·米莱诺娃·马里诺娃 普罗夫迪夫 4000,布尔。瓦西尔·阿普洛夫 115 0899803698

所以我尝试逐行读取 xml 文件并在标签中获取元素名称或属性名称 - 以及文本框中的元素值或属性值(以便用户进行更改)

  int i = 0;

            XmlTextReader rdr = new XmlTextReader("E:/Tanya Documents/Stanga1Projects/XML_project_Tanya_Marinova/cv.xml");
            while (rdr.Read())
            {
                if (rdr.NodeType == XmlNodeType.Element)
                {
                    Label nodeName = new Label();
                    nodeName.Text = rdr.LocalName+":  ";
                    Page.FindControl("form1").Controls.Add(nodeName);

                     if (i != 1)
                     {
                         XmlReader pReader = rdr.ReadSubtree();
                         while (pReader.Read())
                         {
                             if (pReader.NodeType == XmlNodeType.Text)
                             {
                                 TextBox txtBox = new TextBox();
                                 txtBox.Text = rdr.Value;
                                 Page.FindControl("form1").Controls.Add(txtBox);

                             }
                             if (pReader.NodeType == XmlNodeType.Element)
                             {
                                 for (int t = 0; t < rdr.AttributeCount; t++)
                                 {
                                     /* ...Here I want a label with attribute name not value)*/
                                     TextBox txbAttribute = new TextBox();
                                     txbAttribute.Text = rdr.GetAttribute(t);
                                     Page.FindControl("form1").Controls.Add(txbAttribute);

                                 }
                             }
                         }
                     }

                    Page.FindControl("form1").Controls.Add(new LiteralControl("<br />"));



                }
                i++;
}

一切正常,但是当我到达“教育”元素时——它有 childNodes——带有属性的元素——我只能用 'getAttributes' 方法获得属性值,但我无法得到他们的名字

非常感谢

【问题讨论】:

    标签: c# xml xml-parsing


    【解决方案1】:

    您能尝试一下这些方法吗?从MSDN 的示例中稍作修改以适应您的实现

            if (pReader.NodeType == XmlNodeType.Element)
            {
                if (pReader.HasAttributes)
                {
                    while (pReader.MoveToNextAttribute())
                    {
                        /* ...Here I want a label with attribute name not value)*/
                        Label lblAttribute = new Label();
                        lblAttribute.Text = pReader.Name;
                        TextBox txbAttribute = new TextBox();
                        txbAttribute.Text = pReader.Value;
                        Page.FindControl("form1").Controls.Add(txbAttribute);
                    }   
    
                    // Move the reader back to the element node.
                    reader.MoveToElement();
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-09
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      • 2011-10-10
      • 2011-11-30
      相关资源
      最近更新 更多