简体   繁体   English

无法获取xml元素的属性名称

[英]Can't get attribute name of xml element

hello I got this xml file 你好,我得到了这个xml文件

Tanya Milenova Marinova Plovdiv 4000, bul. Tanya Milenova Marinova Plovdiv 4000,bul。 Vasil Aprilov 115 0899803698 瓦西尔·阿普里索夫115 0899803698

So I try to read the xml file line by line and get element name or attribute name in a label - and the element value or attribute value in a textbox (so that the user can make changes) 因此,我尝试逐行读取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++;
}

Everything works fine but when I get to 'education' element - which has childNodes - element with attributes - I can get only attribute value with 'getAttributes' method but I can't get their name 一切正常,但是当我进入'education'元素-具有childNodes-带有属性的元素时-我只能通过'getAttributes'方法获得属性值,但无法获得其名称

Thank you very much 非常感谢你

Could you try something along these lines? 您能按照这些方法尝试一些吗? Slight modification to fit your implementation from the example at MSDN 略微修改以适合您在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();
            }
        }

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

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