Help read an XML Doc

Rate It (1)

Last post 09-10-2008 5:00 AM by Samu Zhang - MSFT. 4 replies.

Sort Posts:

  • Help read an XML Doc

    09-04-2008, 8:36 PM
    • Member
      point Member
    • labza
    • Member since 09-05-2008, 12:07 AM
    • Posts 2

    I'm trying to read some specific nodes i the following XML document and I'm having difficulties reding the Field nodes. I would like to read and capture all the nodes and child nodes between the <errors> node to be able to store in database

    <ServerErrors>
        <Error>
          <CODE>AABB</CODE>
          <POSITION>52</POSITION>
          <MESSAGE>message1</MESSAGE>
          <KeyFields>
            <Field>
              <NAME>ModeID</NAME>
              <VALUE>44</VALUE>
            </Field>
            <Field>
              <NAME>TypeID</NAME>
              <VALUE>11</VALUE>
            </Field>
            <Field>
              <NAME>otherType</NAME>
              <VALUE>22</VALUE>
            </Field>
            <Field>
              <NAME>RunDate</NAME>
              <VALUE>2007/07/10</VALUE>
            </Field>
            <Field>
              <NAME>ClassCode</NAME>
              <VALUE>TT</VALUE>
            </Field>
          </KeyFields>
        </Error>
        <Error>
          <CODE>AABB</CODE>
          <POSITION>52</POSITION>
          <MESSAGE>
          message1
        </MESSAGE>
          <KeyFields>
            <Field>
              <NAME>ModeID</NAME>
              <VALUE>42</VALUE>
            </Field>
            <Field>
              <NAME>TypeID</NAME>
              <VALUE>12</VALUE>
            </Field>
            <Field>
              <NAME>otherType</NAME>
              <VALUE>21</VALUE>
            </Field>
            <Field>
              <NAME>RunDate</NAME>
              <VALUE>2007/07/10</VALUE>
            </Field>
            <Field>
              <NAME>ClassCode</NAME>
              <VALUE>TF</VALUE>
            </Field>
          </KeyFields>
        </Error>
    <ServerErrors>

                    XmlNodeList errorNodes = doc.GetElementsByTagName("Error");
                    foreach (XmlNode errorNode in errorNodes)
                    {
                       
    
                        foreach (XmlNode childNode in errorNode.ChildNodes)
                        {
                            Code = errorNode["CODE"].InnerText;
                            Position = errorNode["POSITION"].InnerText;
                            Message = errorNode["MESSAGE"].InnerText;
                            
                         //   if (childNode.Name.Equals("KeyFields"))
                         //   {
                            //    XmlNodeList fieldNodes = doc.GetElementsByTagName("Field");
                           //     foreach (XmlNode fieldNode in fieldNodes)
                           //     {
                           //         foreach (XmlNode fnode in fieldNode.ChildNodes)
                           //         {
    
                           //             cName = fieldNode["NAME"].InnerText;
                           //             cValue = fieldNode["VALUE"].InnerText;
                           //         }
    
                           //     }
                          //  }
    
                            }
                          } 
     

     

     

  • Re: Help read an XML Doc

    09-04-2008, 8:54 PM
    Answer
    • All-Star
      90,903 point All-Star
    • SGWellens
    • Member since 01-02-2007, 9:27 PM
    • Twin Cities, MN
    • Posts 7,417
    • Moderator
      TrustedFriends-MVPs

    Try this way:

     

        protected void Button1_Click(object sender, EventArgs e)
        {
            XmlDocument XDoc = new XmlDocument();
            XDoc.Load(MapPath(@"~\Test.xml"));
    
            String Code, Position, Message, cName, cValue;
    
            XmlNodeList ChildNodes = XDoc.SelectNodes(@"/ServerErrors/Error");
    
            foreach (XmlNode ChildNode in ChildNodes)
            {
                Code = ChildNode["CODE"].InnerText;
                Position = ChildNode["POSITION"].InnerText;
                Message = ChildNode["MESSAGE"].InnerText;
    
                XmlNodeList FieldNodes = ChildNode.SelectNodes(@"KeyFields/Field");
    
                foreach (XmlNode FieldNode in FieldNodes)
                {
                    cName = FieldNode["NAME"].InnerText;
                    cValue = FieldNode["VALUE"].InnerText;
                }                    
            }
    
     
    Steve Wellens

    My blog
  • Re: Help read an XML Doc

    09-04-2008, 11:11 PM
    • Member
      point Member
    • labza
    • Member since 09-05-2008, 12:07 AM
    • Posts 2

    Thank you for your quir reply:

    the loop crushes when using SelecNodes method. the loop continues when I use doc.GetElementsByTagName. My problem is the loop within the FieldNode List. It iterates through all the field element not the <Field> element within each <Error> elements.

     Thanks for youe help again.

     

    Labza

  • Re: Help read an XML Doc

    09-05-2008, 12:56 AM
    Answer
    • Star
      8,703 point Star
    • ksridharbabuus
    • Member since 11-10-2007, 1:02 PM
    • Bangalore, India
    • Posts 1,282

    Hi,

        Code Provided by Steve Wellens is for traversing each and every field of the Error element.

    First Loop will get all the Error Elements and then it will goto each and every Field element within the KeyFields which is a second loop.

    -Sri
    Visit My Blog
    -------------------------------------------------
    If this post was useful to you, please mark it as answer. Thank you!
  • Re: Help read an XML Doc

    09-10-2008, 5:00 AM
    Answer

    Hi labza ,

    labza:
    My problem is the loop within the FieldNode List. It iterates through all the field element not the <Field> element within each <Error> elements.

    If you want to loop through the field nodes in Error node, to use xpath is a good idea.

                XmlDocument doc = new XmlDocument();
                doc.Load(Server.MapPath("XMLFile.xml"));
    
                XmlNodeList lst = doc.SelectNodes("//Field");
    
                foreach (XmlNode field in lst)
                {
                    string NAME = field.SelectSingleNode("NAME").InnerText;
                    string VALUE = field.SelectSingleNode("VALUE").InnerText;
                }

     

     


    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
Page 1 of 1 (5 items)