[dba-VB] XML LINQ Help!

Jim DeMarco jdthree at frontiernet.net
Tue Sep 29 06:50:22 CDT 2009


Michael,

Something like this should work for your first questions.  The Races nodes
I've not figured out and I have a similar file that I'd like to read with
LINQ (so if anyone knows how to get into the nested elements please
respond).

To get Summary attribute:

        XElement myNode = XElement.Load(@"myfile.xml");

        var query = from summary in myNode.Elements("Summary")

        select summary; 

       foreach (var summary in query) 

        {
            m_summaryMID = (string)summary.Attribute("MID").Value;
        }

To get VenueName:

        XElement myNode = XElement.Load(@"myfile.xml");


        var query = from summary in myNode.Elements("Summary ")

                    where (string) summary.Attribute("MID").Value ==
m_summaryMID             

        select summary; 

       foreach (var summary in query) 

        {
            m_venueName = summary.Element("VenueName").Value;
        }

I'd point you to a good tutorial but this info is about as much as I found.
If anyone else knows of something a bit deeper I'm sure Michael and I would
appreciate it.

HTH,

Jim DeMarco

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Michael Maddison
Sent: Monday, September 28, 2009 11:34 PM
To: Discussion concerning Visual Basic and related programming issues.
Subject: [dba-VB] XML LINQ Help!

Hi guys,

VS2008 C#

I've grabbed some XML data from a web service and I need to do some
operations on it. This is all new to me and I'm a bit lost trying to follow
MSDN docs.
The data looks like this.

<Data>
  <Summary MID="SR_20090924">
    <VenueName>HAWKESBURY (NSW)</VenueName>
    <Type>R</Type>
    <Events>9</Events>
    <Track>GOOD    </Track>
    <Weather>FINE    </Weather>
    <Races>
      <Race RID="SR_20090924_01">
        <Name>MADE JEWELLERY F&M MDN HCP</Name>
        <Distance>1400m     </Distance>
        <Class>
        </Class>
        <RunnerNo Acceptors="13">13</RunnerNo>
        <JumpTime>12:10</JumpTime>
        <TimeToJump>+02:35:19</TimeToJump>
        <RaceStatus>Betting Open</RaceStatus>
      </Race>
  </Summary>
  <Summary ...>
	//more
  </Summary>
</Data>

I can load the xml using XElement racesFromFile = XElement.Load ( @path );

I need to do a variety of things.

How do I get all the Summary Attributes?  MID = ?

How do I get VenueNAme?

And all the raceIDs?

Then using LINQ(?) get data specific to a RaceID?

Maybe a pointer to a good tutorial would be appreciated.

Cheers

Michael M





_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com




More information about the dba-VB mailing list