Ask a questionAsk a question
 

AnswerXML Parsing

  • Tuesday, November 03, 2009 10:37 PMjulian ustiyanovych Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi guys,

    i'd like to ask you, how can I parse xml file below:

    <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 need to have two Lists (Name and Email).

    I would like to see how do you do that.


    thank you in advance
    J.

Answers

  • Tuesday, November 03, 2009 11:18 PMStephen Cleary Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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

All Replies