TRY THIS code as IS.
string myXml =
"<sport name=\"Football\" sportid=\"3893\">" +
"<group name=\"Argentina - Primera Divison\" groupid=\"12322\">" +
"<event name=\"Atletico Rafaela v Union de Santa Fe\" eventid=\"86376\" closetime=\"11/03/2012 23:10\">" +
"<bettype name=\"Versus (with Draw)\" eventid=\"13986376\" etid=\"10\">" +
"<bet outcome_name=\"Atletico Rafaela\" id=\"69702140\" odd=\"2.00\" status=\"Close\"/>" +
"<bet outcome_name=\"X\" id=\"69702141\" odd=\"3.10\" status=\"Open\"/>" +
"<bet outcome_name=\"Union de Santa Fe\" id=\"69702142\" odd=\"3.75\" status=\"Open\"/>" +
"</bettype>" +
"<bettype name=\"Half Time\" eventid=\"13986379\" etid=\"120\">" +
"<bet outcome_name=\"1\" id=\"69702149\" odd=\"2.75\" status=\"Open\"/>" +
"<bet outcome_name=\"X\" id=\"69702150\" odd=\"2.00\" status=\"Open\"/>" +
"<bet outcome_name=\"2\" id=\"69702151\" odd=\"3.75\" status=\"Open\"/>" +
"</bettype>" +
"<bettype name=\"Double Chance\" eventid=\"13986378\" etid=\"140\">" +
"<bet outcome_name=\"1 or X\" id=\"69702146\" odd=\"1.22\" status=\"Open\"/>" +
"<bet outcome_name=\"X or 2\" id=\"69702147\" odd=\"1.70\" status=\"Open\"/>" +
"<bet outcome_name=\"1 or 2\" id=\"69702148\" odd=\"1.30\" status=\"Open\"/>" +
"</bettype>" +
"</event>" +
"</group>" +
"</sport>";
XDocument doc = XDocument.Parse(myXml);
var items = from list in doc.Descendants("bettype")
from item in list.Elements("bet")
select item;
/*
* That will cope with multiple "bet" elements, and won't find "event"
* elements except directly under "bettype" ones. If you don't care about
* those finer points you could just use:
*/
doc = XDocument.Parse(myXml);
var items_bet = doc.Descendants("bet").Attributes();
// Execute the query - READ ALL ATRIBUTES
// I have read only attibutes as none of the element has Direct value in it.
Response.Write("<table border='1'>");
foreach (var bets in items_bet)
{
Response.Write("<tr><td>" + bets.Name + "</td><td>" + bets.Value + "<td></tr>");
}
Response.Write("</table>");
Please mark this post as Answer if it is of help to you!
I would love to change the world, but they wont give me the source code.
Marked as answer by khayalian on Mar 30, 2012 04:50 AM
because this is just a small part, because i have more than 72 leagues and many many games, i need to import it from outer xml data and show it in a repeater or datalist.
If all of the xml data contents into an existing xml file,no need for you to add this again but just use XDocument.Load instead of XDocument.Parse——the first method means you can load an existing xml file,while the latter means you can directly parse an
xml string。
Reguards!
Marked as answer by khayalian on Mar 30, 2012 04:50 AM
Thank you for solving my problem, but ineed to show the data of the xml in aspx page using xPath("@bet"), instead of Rsponse.Write, and i need to show all the info in this small xml file.
group=>groupname and =>groupid
event=>eventname and eventid
khayalian
Member
127 Points
143 Posts
XML to linq
Mar 28, 2012 06:06 PM|LINK
Hello i have this XML how can i use linq and show all the nodes, attributes and elements using linq?
<sport name="Football" sportid="3893">
<group name="Argentina - Primera Divison" groupid="12322">
<event name="Atletico Rafaela v Union de Santa Fe" eventid="86376" closetime="11/03/2012 23:10">
<bettype name="Versus (with Draw)" eventid="13986376" etid="10">
<bet outcome_name="Atletico Rafaela" id="69702140" odd="2.00" status="Close"/>
<bet outcome_name="X" id="69702141" odd="3.10" status="Open"/>
<bet outcome_name="Union de Santa Fe" id="69702142" odd="3.75" status="Open"/>
</bettype>
<bettype name="Half Time" eventid="13986379" etid="120">
<bet outcome_name="1" id="69702149" odd="2.75" status="Open"/>
<bet outcome_name="X" id="69702150" odd="2.00" status="Open"/>
<bet outcome_name="2" id="69702151" odd="3.75" status="Open"/>
</bettype>
<bettype name="Double Chance" eventid="13986378" etid="140">
<bet outcome_name="1 or X" id="69702146" odd="1.22" status="Open"/>
<bet outcome_name="X or 2" id="69702147" odd="1.70" status="Open"/>
<bet outcome_name="1 or 2" id="69702148" odd="1.30" status="Open"/>
</bettype>
</event>
</group>
Thank you
kavita_khand...
Star
9767 Points
1930 Posts
Re: XML to linq
Mar 29, 2012 12:32 PM|LINK
TRY THIS code as IS. string myXml = "<sport name=\"Football\" sportid=\"3893\">" + "<group name=\"Argentina - Primera Divison\" groupid=\"12322\">" + "<event name=\"Atletico Rafaela v Union de Santa Fe\" eventid=\"86376\" closetime=\"11/03/2012 23:10\">" + "<bettype name=\"Versus (with Draw)\" eventid=\"13986376\" etid=\"10\">" + "<bet outcome_name=\"Atletico Rafaela\" id=\"69702140\" odd=\"2.00\" status=\"Close\"/>" + "<bet outcome_name=\"X\" id=\"69702141\" odd=\"3.10\" status=\"Open\"/>" + "<bet outcome_name=\"Union de Santa Fe\" id=\"69702142\" odd=\"3.75\" status=\"Open\"/>" + "</bettype>" + "<bettype name=\"Half Time\" eventid=\"13986379\" etid=\"120\">" + "<bet outcome_name=\"1\" id=\"69702149\" odd=\"2.75\" status=\"Open\"/>" + "<bet outcome_name=\"X\" id=\"69702150\" odd=\"2.00\" status=\"Open\"/>" + "<bet outcome_name=\"2\" id=\"69702151\" odd=\"3.75\" status=\"Open\"/>" + "</bettype>" + "<bettype name=\"Double Chance\" eventid=\"13986378\" etid=\"140\">" + "<bet outcome_name=\"1 or X\" id=\"69702146\" odd=\"1.22\" status=\"Open\"/>" + "<bet outcome_name=\"X or 2\" id=\"69702147\" odd=\"1.70\" status=\"Open\"/>" + "<bet outcome_name=\"1 or 2\" id=\"69702148\" odd=\"1.30\" status=\"Open\"/>" + "</bettype>" + "</event>" + "</group>" + "</sport>"; XDocument doc = XDocument.Parse(myXml); var items = from list in doc.Descendants("bettype") from item in list.Elements("bet") select item; /* * That will cope with multiple "bet" elements, and won't find "event" * elements except directly under "bettype" ones. If you don't care about * those finer points you could just use: */ doc = XDocument.Parse(myXml); var items_bet = doc.Descendants("bet").Attributes(); // Execute the query - READ ALL ATRIBUTES // I have read only attibutes as none of the element has Direct value in it. Response.Write("<table border='1'>"); foreach (var bets in items_bet) { Response.Write("<tr><td>" + bets.Name + "</td><td>" + bets.Value + "<td></tr>"); } Response.Write("</table>");I would love to change the world, but they wont give me the source code.
khayalian
Member
127 Points
143 Posts
Re: XML to linq
Mar 29, 2012 10:15 PM|LINK
Hello Thank you very much, but do i need to add
string myXml = "<sport name=\"............................</sport>because this is just a small part, because i have more than 72 leagues and many many games, i need to import it from outer xml data and show it in a repeater or datalist.Thank you waiting for some codes.ShantDecker Dong ...
All-Star
118619 Points
18779 Posts
Re: XML to linq
Mar 30, 2012 01:32 AM|LINK
Hello:)
If all of the xml data contents into an existing xml file,no need for you to add this again but just use XDocument.Load instead of XDocument.Parse——the first method means you can load an existing xml file,while the latter means you can directly parse an xml string。
Reguards!
khayalian
Member
127 Points
143 Posts
Re: XML to linq
Mar 31, 2012 07:31 PM|LINK
Hello
Thank you for solving my problem, but ineed to show the data of the xml in aspx page using xPath("@bet"), instead of Rsponse.Write, and i need to show all the info in this small xml file.
and all other informations too
Thank you waiting for your kind replay