Getting the last xpathSelectElement().Attribute()?.Value c#

  • Thread starter Thread starter CuriousCoder15
  • Start date Start date
C

CuriousCoder15

Guest
Hi,

I asked a question at before:

Getting an xml node that appears more than once linq c#


But I have made an error, not sure if I should ask on the above link as I marked it as answered so I'm asking a new one, hope this is ok...

Here is my XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Racing SYSTEM "Racing.dtd">
<Racing timestamp="20190322T150832+0100">
<Meeting id="109" status="" date="20190322" Destination="UK">
<Event id="88" status="" date="20190322" noContestants="5" time="1510+0100">
<Contestant id="22" status="" name="Troy Mc Clean">
<Shirt number="1"/>
<Player id="11" name="Morgan Spice">
</Player>
<Coach id="13" name="John Smith"/>
<Game timestamp="20190322T150333+0100" EventNumber="1">
<Odds denom="8" numer="15"/>
</Game>
<Game timestamp="20190322T150539+0100" EventNumber="1">
<Odds denom="4" numer="7"/>
</Game>
<Game timestamp="20190322T150546+0100" EventNumber="1">
<Odds denom="8" numer="15"/>
</Game>
<Game timestamp="20190322T150703+0100" EventNumber="1">
<Odds denom="1" numer="7"/>
</Game>
</Contestant>
<Contestant></Contestant>
<Contestant></Contestant>
<Contestant></Contestant>
<Contestant></Contestant>
</Event>
<Event></Event>
<Event></Event>
<Event></Event>
</Meeting>
</Racing>

I am using XDocument and XPath, I would like to capture the:

<Meeting Destination="UK",

<Event time="1510" and noContestants="5"

<Contestant status="" and name="Troy mc Clean"

<shirt number="1"

<Player name="Morgan Spice"

and the last <Game timestamp="20190322T150333+0100" and last <Odds numer="7" denom="1"

Here is my code so far:


var Elements = doc.XPathSelectElements("/Racing/Meeting");
var items = from m in Elements
select new
{
dest = m.Attribute("Destination")?.Value,

events = from e in
m.XPathSelectElements("/Racing/Meeting/Event")
select new
{
nocontest = e.Attribute("noContestants")?.Value,
time = e.Attribute("time")?.Value,

contestant = from c in
m.XPathSelectElements("/Racing/Meeting/Event/Contestant")
select new
{
contName = c.Attribute("name")?.Value,
status = c.Attribute("status")?.Value,
shirtNo = c.XPathSelectElement("Shirt").Attribute("number")?.Value,

// Here I want to get the last timestamp from <Game and the last numer and denom from <Odds so it would

be: timestamp="20190322T150703+0100" numer="7" denom="1"


}

}

{



Can someone show me how to get the part please? I have tried

denom = c.XPathSelectElement("/Racing/Meeting/Event/Contestant/Game/[Odds ='denom'][position() =last()]").Attribute("denom")?.Value,


There are lots of Contestants and Events, thank you for your help.





CuriousCoder

Continue reading...
 
Back
Top