Parsing XML elements and attributes

Last post 07-04-2008 2:10 PM by Onimusha_kiyoko. 6 replies.

Sort Posts:

  • Parsing XML elements and attributes

    07-04-2008, 10:20 AM

    Hi Guys,

    This is my first post. I've been using the forum for a while to get info so I'm hoping you might be able to point me in the right direction here!

    My problem is that I have XML (see below) that I would like to go through but I am confused as to how best to do this. I have spent the last few days looking online and reading up on how to do this but I'm getting nowhere fast. As you can see from the XML (which is a dummy version) I have values I need to get within tags and also outside of tags.

    For example, using the XML sample below, I might want to get the "test guy" value out of the Person tag, the address details within LocationDetails and nothing else. At the moment I can display all elements from the XML but then I don't get the values in the address tags OR I can target a specific tag id and get that value (i.e. the address) but then I don't get the other information like the person's name.

    Is there a way to use xDoc.GetElementsByTagName("Person") and get the Text value of "test guy"? This would be great if I could do this as I can just specify the actual tags I want from the xml.

    I am building this in Visual Studio 2008 using C# but on the ver 2 framework.

    Thanks in advance for looking at this with me.

    Moderators - If this is not in the right section than I'm sorry, feel free to move to a more appropriate location.

    <messages>
      <Person id="0" text="test guy" fieldid="1"></Person>
      <LocationDetails>
            <address1>place name</address1>
            <address2></address2>
            <town>test town</town>
            <state>NY</state>
            <country>US</country>
      </LocationDetails>
      <Schedules>
            <Schedule>
                <Times>
                    <ScheduleTime Mon="true" Start="09:00" End="17:30"></ScheduleTime>
                    <ScheduleTime Tue="true" Start="09:00" End="17:30"></ScheduleTime>
                    <ScheduleTime Weds="true" Start="09:00" End="17:30"></ScheduleTime>
                    <ScheduleTime Thurs="true" Start="09:00" End="17:30"></ScheduleTime>
                    <ScheduleTime Fri="true" Start="09:00" End="17:30"></ScheduleTime>
                    <ScheduleTime Fri="false" Start="09:00" End="17:30"></ScheduleTime>
                    <ScheduleTime Fri="false" Start="09:00" End="17:30"></ScheduleTime>
                </Times>
            </Schedule>
      </Schedules>
    </messages>

  • Re: Parsing XML elements and attributes

    07-04-2008, 10:52 AM
    • Loading...
    • TimTrott
    • Joined on 12-04-2007, 10:51 AM
    • Posts 36
    I think you should be able to use an XmlDocument and the SelectSingleNode method, off the top of my head this should work:

    string text = xmlDoc.SelectSingleNode("/messages/Person/@text").InnerText;

    string address1 = xmlDoc.SelectSingleNode("/messages/LocationDrtails/address1").InnerText;

    If you need more help I can do up an example when I have access to VS.

     

    Regards,

    Tim

    C# Tutorials - Learn ASP.Net, ADO.Net and the .Net Framework.
  • Re: Parsing XML elements and attributes

    07-04-2008, 11:45 AM

     Hi Tim,

     Thanks for the quick reply! I have tried to use the following code but I keep getting an error - Object reference not set to an instance of an object.

    My code is as follows:

    XmlDocument xDoc = new XmlDocument();
    xDoc.LoadXml(results);
    
    string text = xDoc.SelectSingleNode("/messages/Person/@text").InnerText;
            
    Response.Write(text);

    I have tried some other variations but I still get the same error message, any thoughts?

    Thanks,

    Rich 

  • Re: Parsing XML elements and attributes

    07-04-2008, 12:07 PM
    Answer
    • Loading...
    • TimTrott
    • Joined on 12-04-2007, 10:51 AM
    • Posts 36

    Strange, is it throwing an exception on the SelectSingeNode line? You will get an object referece not set message if the attribute "text" does not exist.

    Here is some sample code for a console app using a hard coded xml you provided.

     

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          string results = @"<messages>
      <Person id='0' text='test guy' fieldid='1'></Person>
      <LocationDetails>
            <address1>place name</address1>
            <address2></address2>
            <town>test town</town>
            <state>NY</state>
            <country>US</country>
      </LocationDetails>
      <Schedules>
            <Schedule>
                <Times>
                    <ScheduleTime Mon='true' Start='09:00' End='17:30'></ScheduleTime>
                    <ScheduleTime Tue='true' Start='09:00' End='17:30'></ScheduleTime>
                    <ScheduleTime Weds='true' Start='09:00' End='17:30'></ScheduleTime>
                    <ScheduleTime Thurs='true' Start='09:00' End='17:30'></ScheduleTime>
                    <ScheduleTime Fri='true' Start='09:00' End='17:30'></ScheduleTime>
                    <ScheduleTime Fri='false' Start='09:00' End='17:30'></ScheduleTime>
                    <ScheduleTime Fri='false' Start='09:00' End='17:30'></ScheduleTime>
                </Times>
            </Schedule> 
      </Schedules> 
    </messages>";
    
    
          XmlDocument xDoc = new XmlDocument();
          xDoc.LoadXml(results);
    
          string text = xDoc.SelectSingleNode("/messages/Person/@text").InnerText;
    
          Console.WriteLine(text);
          Console.ReadLine();
        }
      }
    }
    

     

    See if that works, or double check your input xml contains the text attribute on the /messages/Person node.

    C# Tutorials - Learn ASP.Net, ADO.Net and the .Net Framework.
  • Re: Parsing XML elements and attributes

    07-04-2008, 12:39 PM

     Hi Tim,

     I have it working perfectly now. Thanks so much for your great advice!

    Just one quick question - 

    On my real XML data when I'm selecting the particular node the data I want could be 7 or 8 elements in which means I end up with a SelectSingleNode that looks like:

    xDoc.SelectSingleNode(/maintag/tag1/tag2/tag3/tag4/tag5/tag6/tagWanted).InnerText;

    Performance wise is this ok to do or should I be using a different way to grab the values of the inner elements?

     If for example I would be using the xml in this post and I wanted the ScheduleTime for Mon only, is it possible to just tell the parser to pick elements from the <Times> tag and then use the SelectSingleNode to get the exact row I want?

     Thanks,

    Rich

  • Re: Parsing XML elements and attributes

    07-04-2008, 1:56 PM
    • Loading...
    • TimTrott
    • Joined on 12-04-2007, 10:51 AM
    • Posts 36

    I am not aware of a performance difference between  xDoc.SelectSingleNode(/maintag/tag1/tag2/tag3/tag4/tag5/tag6/tagWanted).InnerText; and xDoc.SelectSingleNode(/maintag/tagWanted).InnerText; for single requests, but if you are processing many entries, especially if they are repeating elements, you may want to extract and work with an XmlNode object or look at XPathDocument instead.

    C# Tutorials - Learn ASP.Net, ADO.Net and the .Net Framework.
  • Re: Parsing XML elements and attributes

    07-04-2008, 2:10 PM

    TimTrott:

    I am not aware of a performance difference between  xDoc.SelectSingleNode(/maintag/tag1/tag2/tag3/tag4/tag5/tag6/tagWanted).InnerText; and xDoc.SelectSingleNode(/maintag/tagWanted).InnerText; for single requests, but if you are processing many entries, especially if they are repeating elements, you may want to extract and work with an XmlNode object or look at XPathDocument instead.

     

     Thanks for all your help Tim, appreciate it!

     Rich

Page 1 of 1 (7 items)
Microsoft Communities
Page view counter