none
Read Nested NodeList XML in C#

    Question

  • Hello all,
    I have an XML string which is below:

    "<ContactsList>
    "<Contacts>"
    <Contact><ItemId>" + itemId + "</ItemId><Name>" + strName "</Name><Company>" + strCompany + "</Company><MobileNumber>" + strMobileNum + "</MobileNumber></Contact>
    <Contact><ItemId>" + itemId + "</ItemId><Name>" + strName + "</Name><Company>" + strCompany + "</Company><MobileNumber>" + strMobileNum + "</MobileNumber></Contact>
    <Contact>......</Contacts></Contact>
    -------
    </ContactsList>"


    What i have to do is loop through the XML string for <
    Contacts> and read all the tags inside each <Contact></Contact>.
    I have load this XML string in an XMLDocument.
    How can i do this? How to use loop or something like this to iterate for <Contact> tags?
    I am using VS 2005 and C#.
    Thanks in advance
    Monday, December 17, 2007 8:18 AM

Answers

  • Oh it was not that difficult to do. I just wrote this:

    XmlElement elm = xmldoc.DocumentElement;//xmldoc is the XmlDocument.
    XmlNodeList nodeList= elm.SelectNodes("ContactsList/Contacts");

    and then used for loop immediately:

    for (int i = 0; i < nodeList.Count; i++)
                {
    string itemNumber1=nodeListIdea.ChildNodes[0].InnerText;
    .....................
    ....................
      }

    and it is done what i wanted. Anybody having the same problem may use this code.

    Monday, December 17, 2007 10:02 AM