locked
looping through inner xml RRS feed

  • Question

  • Hi,

    I have an inner xml from an xml file like this

    <A>1</A>
    <B>2</B>

    How can i loop through each element and get the element name and data. The data above i get is in the inner xml .

    Regards

     


    litu Here
    Wednesday, May 18, 2011 11:17 AM

Answers

  • Hi litusahoo,

    We can use the following code to loop through inner xml, and get the element name and value.

                string xmlstr = @"<site><A>1</A><B>2</B></site>";

                XmlDocument doc = new XmlDocument();

                doc.LoadXml(xmlstr);

                XmlElement root = doc.DocumentElement;

                XmlNodeList elements = root.ChildNodes;

                for (int i = 0; i < elements.Count; i++)

                {

                    Console.WriteLine("Name: {0}, value: {1}", elements[i].Name, elements[i].InnerXml);

                }

    Then the console shows:

    Name:A, value:1

    Name:B, value:2

     

    If you have solved this issue, please mark the useful reply as answer.

     

    Best regards,

    Lucy


    Lucy Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.



    • Proposed as answer by MASNSN Monday, May 23, 2011 1:21 PM
    • Edited by lucy-liu Tuesday, May 24, 2011 3:04 AM
    • Marked as answer by lucy-liu Wednesday, May 25, 2011 6:37 AM
    Monday, May 23, 2011 6:20 AM

All replies


  • Loop through the childnodes property, as shown here: http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.childnodes.aspx

    --
    Mike
    Wednesday, May 18, 2011 11:50 AM
  • Hi litusahoo,

    We can use the following code to loop through inner xml, and get the element name and value.

                string xmlstr = @"<site><A>1</A><B>2</B></site>";

                XmlDocument doc = new XmlDocument();

                doc.LoadXml(xmlstr);

                XmlElement root = doc.DocumentElement;

                XmlNodeList elements = root.ChildNodes;

                for (int i = 0; i < elements.Count; i++)

                {

                    Console.WriteLine("Name: {0}, value: {1}", elements[i].Name, elements[i].InnerXml);

                }

    Then the console shows:

    Name:A, value:1

    Name:B, value:2

     

    If you have solved this issue, please mark the useful reply as answer.

     

    Best regards,

    Lucy


    Lucy Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.



    • Proposed as answer by MASNSN Monday, May 23, 2011 1:21 PM
    • Edited by lucy-liu Tuesday, May 24, 2011 3:04 AM
    • Marked as answer by lucy-liu Wednesday, May 25, 2011 6:37 AM
    Monday, May 23, 2011 6:20 AM
  • Hi lucy

    I will marked it as an answer don't worry lucy ; ) but the code user should be worried and add >"; at the end of the xmlstr string

     

     


    The complexity resides in the simplicity Follow me at: http://smartssolutions.blogspot.com
    Monday, May 23, 2011 1:21 PM
  • Hi MASNSN,

    Thank you very much, I have add >"; at the end of the xmlstr string.

     

    Best regards,

    Lucy


    Lucy Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 24, 2011 3:04 AM