EDN Admin
Well-known member
Hello,
heres the thing. I dont need this pro, but I need to read simple xml for silverlight application. Wanted to do it via LINQ.
Made this:
<pre class="prettyprint private static XDocument xdoc = XDocument.Load("games.xml");
private void sweepButton_Click(object sender, RoutedEventArgs e)
{
var title = from game in xdoc.Elements("Game")
where (string)game.Attribute("id") == "1"
select game.Element("Title").Value;
/*Game thisgame = (from game in xdoc.Descendants("Game")
where game.Attribute("id").Value.Equals("1")
select new Game()
{
Title = game.Element("Title").Value,
Description = game.Element("Description").Value,
Prize = Convert.ToInt32(game.Element("Prize").Value)
}).FirstOrDefault(); gameDescription.Text = thisgame.Description;
*/
//my previous attempt
gameDescription.Text = title.ToString() ;
} public class Game<br/> {<br/> public string Title { get; set; }<br/> public string Description { get; set; }<br/> public int Prize { get; set; }<br/> } [/code]
<pre class="prettyprint <?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Games>
<Game id="1
<Title>Sweepem All</Title>
<Description>Blabla</Description>
<Prize>$10</Prize>
</Game>
<Game id="2
<Title>Jungle crawl</Title>
<Description>Blabla</Description>
<Prize>$7</Prize>
</Game>
</Games>[/code]
And I know! Im getting output: System.Linq.Enumerable+WhereSelectEnumerableIterator2[System.Xml.Linq.XElement,System.String]
I know kind of thats var title isnt the thing i want to convert to string, but have no idea how to take this real title from this xml.
So how to print as a String one element from XDocument via LINQ?
View the full article
heres the thing. I dont need this pro, but I need to read simple xml for silverlight application. Wanted to do it via LINQ.
Made this:
<pre class="prettyprint private static XDocument xdoc = XDocument.Load("games.xml");
private void sweepButton_Click(object sender, RoutedEventArgs e)
{
var title = from game in xdoc.Elements("Game")
where (string)game.Attribute("id") == "1"
select game.Element("Title").Value;
/*Game thisgame = (from game in xdoc.Descendants("Game")
where game.Attribute("id").Value.Equals("1")
select new Game()
{
Title = game.Element("Title").Value,
Description = game.Element("Description").Value,
Prize = Convert.ToInt32(game.Element("Prize").Value)
}).FirstOrDefault(); gameDescription.Text = thisgame.Description;
*/
//my previous attempt
gameDescription.Text = title.ToString() ;
} public class Game<br/> {<br/> public string Title { get; set; }<br/> public string Description { get; set; }<br/> public int Prize { get; set; }<br/> } [/code]
<pre class="prettyprint <?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Games>
<Game id="1
<Title>Sweepem All</Title>
<Description>Blabla</Description>
<Prize>$10</Prize>
</Game>
<Game id="2
<Title>Jungle crawl</Title>
<Description>Blabla</Description>
<Prize>$7</Prize>
</Game>
</Games>[/code]
And I know! Im getting output: System.Linq.Enumerable+WhereSelectEnumerableIterator2[System.Xml.Linq.XElement,System.String]
I know kind of thats var title isnt the thing i want to convert to string, but have no idea how to take this real title from this xml.
So how to print as a String one element from XDocument via LINQ?
View the full article