XML Newbie help!

XML , Csharp , VS Melbourne, Australia
  • 10 years ago

    Hi All,

    I am pretty new to XML and struggling trying to extract some data from an XML document...

    Here is a snippet of the document:

    What I am trying to extract is the PoolType = "WW" and then within that the DivAmount and RunnerNo.

    I have been playing around with GetElementByName and Attributes but just can't seem to find the values.

    Any help would be greatly appreciated :)

    Cheers Dean

  • 10 years ago

    Heya, if you provide a snippet of the XML you are trying to parse, i'll see if i can show you how.

  • 10 years ago

    Hi AmzBee,

    I did put it in here but it must have striped it... here it is again, or it it isn't there I have put it on pastebin.com as well :)

    Appreciate the help, it's doing my head in.

    Dean

    http://pastebin.com/deTEjt3L

    <ResultPlace PlaceNo="1">
    	<Result RunnerNo="6" PoolType="PP" Ref50Pct="N" RefScratched="N" RefAbandon="N" RefNoWinner="N" NoPlacePool="N">
    		<PoolResult PoolType="PP" Dividend="2.40"/>
    	</Result>
    	<Result RunnerNo="6" PoolType="WW" Ref50Pct="N" RefScratched="N" RefAbandon="N" RefNoWinner="N" NoPlacePool="N">
    		<PoolResult PoolType="WW" Dividend="10.00"/>
    	</Result>
    </ResultPlace>
    <ResultPlace PlaceNo="2">
    	<Result RunnerNo="3" PoolType="PP" Ref50Pct="N" RefScratched="N" RefAbandon="N" RefNoWinner="N" NoPlacePool="N">
    		<PoolResult PoolType="PP" Dividend="2.50"/>
    	</Result>
    	<Result RunnerNo="3" PoolType="WW" Ref50Pct="N" RefScratched="N" RefAbandon="N" RefNoWinner="N" NoPlacePool="N">
    		<PoolResult PoolType="WW"/>
    	</Result>
    </ResultPlace>
    <ResultPlace PlaceNo="3">
    	<Result RunnerNo="4" PoolType="PP" Ref50Pct="N" RefScratched="N" RefAbandon="N" RefNoWinner="N" NoPlacePool="N">
    		<PoolResult PoolType="PP" Dividend="1.20"/>
    	</Result>
    	<Result RunnerNo="4" PoolType="WW" Ref50Pct="N" RefScratched="N" RefAbandon="N" RefNoWinner="N" NoPlacePool="N">
    		<PoolResult PoolType="WW"/>
    	</Result>
    </ResultPlace>
    
  • 10 years ago

    To use this example, its important to note i added a single enclosing tag callled results arround your xml, this is so that the xml parser would not throw a tantrum...

    <results>
      <ResultPlace PlaceNo="1">
        <Result RunnerNo="6" PoolType="PP" Ref50Pct="N" RefScratched="N" RefAbandon="N" RefNoWinner="N" NoPlacePool="N">
          <PoolResult PoolType="PP" Dividend="2.40"/>
        </Result>
        <Result RunnerNo="6" PoolType="WW" Ref50Pct="N" RefScratched="N" RefAbandon="N" RefNoWinner="N" NoPlacePool="N">
          <PoolResult PoolType="WW" Dividend="10.00"/>
        </Result>
      </ResultPlace>
      <ResultPlace PlaceNo="2">
        <Result RunnerNo="3" PoolType="PP" Ref50Pct="N" RefScratched="N" RefAbandon="N" RefNoWinner="N" NoPlacePool="N">
          <PoolResult PoolType="PP" Dividend="2.50"/>
        </Result>
        <Result RunnerNo="3" PoolType="WW" Ref50Pct="N" RefScratched="N" RefAbandon="N" RefNoWinner="N" NoPlacePool="N">
          <PoolResult PoolType="WW"/>
        </Result>
      </ResultPlace>
      <ResultPlace PlaceNo="3">
        <Result RunnerNo="4" PoolType="PP" Ref50Pct="N" RefScratched="N" RefAbandon="N" RefNoWinner="N" NoPlacePool="N">
          <PoolResult PoolType="PP" Dividend="1.20"/>
        </Result>
        <Result RunnerNo="4" PoolType="WW" Ref50Pct="N" RefScratched="N" RefAbandon="N" RefNoWinner="N" NoPlacePool="N">
          <PoolResult PoolType="WW"/>
        </Result>
      </ResultPlace>
    </results>
    

    To read this xml in c#, you could do the following...

    public void ReadPools()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("results.xml");
    
        XmlNode results = doc.FirstChild;
    
        foreach (XmlNode node in results.ChildNodes)
        {
            int PlaceNo = int.Parse(node.Attributes["PlaceNo"].Value);
            string message = string.Format("Place No: {0}\n", PlaceNo);
    
            foreach (XmlNode result in node)
            {
                string PoolType = result.Attributes["PoolType"].Value;
                message += string.Format("Pool Type: {0}\n", PoolType);
            }
    
            MessageBox.Show(message);
        }
    
    }
    

    I hope this helps :)

    Aimee.

  • 10 years ago

    Hi Aimee,

    That looks great thanks, but I am still struggling a little bit, maybe the whole XML feed might be a bit clearer... here is one of the feeds I get as an example. Now I have it all reading in fine and converting into an XmlDocument, I just dont understand how to get to the nodes and data properly :-S

    http://unitab.com/data/racing/2011/2/3/AG1.xml

    Regards, Dean

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“An idiot with a computer is a faster, better idiot” - Rich Julius