var xml = XDocument.Load(Server.MapPath("~/countries.xml"));
var states = xml.Descendants("country")
.Where(c => c.Attribute("id").Value.Equals("2"))
.Descendants("state")
.Select(s => s.Value)
.ToList();
var states = from c in loaded.Root.Descendants("country")
where (int)c.Attribute("id") == 2
select (from i in c.Descendants("state")
select i.Value).ToList();
Marked as answer by shakimran on Nov 27, 2012 09:19 AM
List<string> states = (from c in doc.Descendants("country")
from s in s.Descendants("state")
where c.Attribute("id").Value == "2"
select s.Value).ToList<string>();
YCS
Please try the answer for the post and finally Don't forget to click “Mark as Answer” on the post that helped you.
Marked as answer by shakimran on Nov 27, 2012 01:55 PM
List<string> states = (from c in doc.Descendants("country")
from s in s.Descendants("state")
where c.Attribute("id").Value == "2"
select s.Value).ToList<string>();
That's virtually identical to what's already been posted. How does it differ from the other sample of query syntax provided by sriharsha2410? Does your version offer some performance improvements perhaps? Does it correct an error in the other sample?
How does it differ from the other sample of query syntax provided by sriharsha2410?
@Mikesdonetting, im sure both the examples are same. i opened the question when it was first posted there were no answers and when i posted my answer, you answer was already there at the top.
@sriharsha2410 That happens quite a lot. My reply showed how to achieve the requirement using chained LINQ extension method calls. Yours shows how to achieve it using Linq Query Syntax. So the answers differ, although they both solve the problem. My point
was that there was no need for chandrashekar to post virtually the same code as yours an hour later.
When I opened the post for answering, I did not see it was already answered and It was not answered yet. But could not revert it after submitting/posting the answer.
YCS
Please try the answer for the post and finally Don't forget to click “Mark as Answer” on the post that helped you.
shakimran
Member
330 Points
149 Posts
Parsing xml data using linq
Nov 27, 2012 06:59 AM|LINK
HI,
I have xml file like this,
<countries>
<country id="1">
<state>M</state>
<state>N</state>
<state>B</state>
<state>V</state>
</country>
<country id="2">
<state>R</state>
<state>E</state>
<state>W</state>
<state>Q</state>
</country>
<country id="3">
<state>H</state>
<state>G</state>
<state>F</state>
<state>D</state>
<state>A</state>
</country>
</countries>
Now i want to write a linq query which returns all states name whrere country id is 2
umairaslam22
Contributor
3990 Points
845 Posts
Re: Parsing xml data using linq
Nov 27, 2012 07:25 AM|LINK
You have to use xml reader or string reader to read xml nodes then fill it in entity
it might help you
http://www.dotnetcurry.com/ShowArticle.aspx?ID=564
MCP
Blog
Please remember to Mark as answer if any post help you , it help others to find right solution in less time
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: Parsing xml data using linq
Nov 27, 2012 07:25 AM|LINK
var xml = XDocument.Load(Server.MapPath("~/countries.xml")); var states = xml.Descendants("country") .Where(c => c.Attribute("id").Value.Equals("2")) .Descendants("state") .Select(s => s.Value) .ToList();Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
sriharsha241...
Member
414 Points
109 Posts
Re: Parsing xml data using linq
Nov 27, 2012 07:34 AM|LINK
hope this helps
var states = from c in loaded.Root.Descendants("country") where (int)c.Attribute("id") == 2 select (from i in c.Descendants("state") select i.Value).ToList();chandrasheka...
Star
10258 Points
1760 Posts
Re: Parsing xml data using linq
Nov 27, 2012 08:30 AM|LINK
Hi
Try the following as well:
List<string> states = (from c in doc.Descendants("country") from s in s.Descendants("state") where c.Attribute("id").Value == "2" select s.Value).ToList<string>();Please try the answer for the post and finally Don't forget to click “Mark as Answer” on the post that helped you.
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: Parsing xml data using linq
Nov 27, 2012 08:49 AM|LINK
That's virtually identical to what's already been posted. How does it differ from the other sample of query syntax provided by sriharsha2410? Does your version offer some performance improvements perhaps? Does it correct an error in the other sample?
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
sriharsha241...
Member
414 Points
109 Posts
Re: Parsing xml data using linq
Nov 27, 2012 09:12 AM|LINK
@Mikesdonetting, im sure both the examples are same. i opened the question when it was first posted there were no answers and when i posted my answer, you answer was already there at the top.
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: Parsing xml data using linq
Nov 27, 2012 10:25 AM|LINK
@sriharsha2410 That happens quite a lot. My reply showed how to achieve the requirement using chained LINQ extension method calls. Yours shows how to achieve it using Linq Query Syntax. So the answers differ, although they both solve the problem. My point was that there was no need for chandrashekar to post virtually the same code as yours an hour later.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
chandrasheka...
Star
10258 Points
1760 Posts
Re: Parsing xml data using linq
Nov 27, 2012 12:10 PM|LINK
Hi,
When I opened the post for answering, I did not see it was already answered and It was not answered yet. But could not revert it after submitting/posting the answer.
Please try the answer for the post and finally Don't forget to click “Mark as Answer” on the post that helped you.