简体   繁体   中英

Can't get attribute name of xml element

hello I got this xml file

Tanya Milenova Marinova Plovdiv 4000, bul. Vasil Aprilov 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)

  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

Thank you very much

Could you try something along these lines? Slight modification to fit your implementation from the example at 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();
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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