XML Parsing
- Hi guys,
i'd like to ask you, how can I parse xml file below:
I need to have two Lists (Name and Email).<LostPeopleInfo> <LostPeople name="Jennifer Rodriges" email="rodrigesj@gmail.com"/> <LostPeople name="Pitter Pen" email="penp@gmail.com"/> <LostPeople name="John Willson" email="willsonj@gmail.com"/> <LostPeople name="Clara Schneider" email="schneiderc@gmail.com"/> <LostPeople name="Urlike Uerb" email="uerbu@gmail.com"/> <LostPeople name="Piter Koch" email="kochp@gmail.com"/> <LostPeople name="Annet Flueghafen" email="flueghafena@gmail.com"/> <LostPeople name="Piter von Roberts" email="robertsp@gmail.com"/> </LostPeopleInfo>
I would like to see how do you do that.
thank you in advance
J.
Answers
- var names = doc.Descendants("LostPeople").Select(x => (string)(x.Attribute("name")));
var emails = doc.Descendants("LostPeople").Select(x => (string)(x.Attribute("email")));
The main difference from what you posted is that "names" and "emails" are stored as queries instead of arrays, so they can be lazy-evaluated.
-Steve
Programming blog: http://nitoprograms.blogspot.com/
Including my TCP/IP .NET Sockets FAQ
Microsoft Certified Professional Developer- Marked As Answer byjulian ustiyanovych Tuesday, November 03, 2009 11:55 PM
All Replies
- Could you guys propose better solution ?
var nodes = from element in doc.Descendants("LostPeople") let att = element.Attribute("name") where att != null select att.Value; string[] names = nodes.ToArray();
thank you in advance. - var names = doc.Descendants("LostPeople").Select(x => (string)(x.Attribute("name")));
var emails = doc.Descendants("LostPeople").Select(x => (string)(x.Attribute("email")));
The main difference from what you posted is that "names" and "emails" are stored as queries instead of arrays, so they can be lazy-evaluated.
-Steve
Programming blog: http://nitoprograms.blogspot.com/
Including my TCP/IP .NET Sockets FAQ
Microsoft Certified Professional Developer- Marked As Answer byjulian ustiyanovych Tuesday, November 03, 2009 11:55 PM
- thank you Stephan !!! looks really nice.
Julian - ups sorry I did mistake in your name Steve :)


