0

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

1 Answer 1

1

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();
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.