Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I use linq to xml to get the list of names

This is in an xml file called:
Names.xml
XML
<names>
<name>john brown 27 years old</name>
<name>jo lewis 21 years old</name>
<name>mike roberts 30 years old</name>
<name>Dustin links 33 years old</name>
</names>

I would like to get the following:

john brown 27 years old
jo lewis 21 years old
mike roberts 30 years old
Dustin links 33 years old

Thank you
Posted
Updated 16-Dec-10 2:19am
v3
Comments
Toniyo Jackson 16-Dec-10 8:18am    
Always write your code inside code block
Hiren solanki 16-Dec-10 9:29am    
see my updated answer. if helped accept it.

Is it really that hard to type "google.com" in your browser?

http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx[^]
 
Share this answer
 
C#
XElement xdoc = XElement.Parse(@"<names>
                                            <name>john brown 27 years old</name>
                                            <name>jo lewis 21 years old</name>
                                            <name>mike roberts 30 years old</name>
                                            <name>Dustin links 33 years old</name>
                                          </names>");
        var names = from name in xdoc.Elements("name")
                    select name;
        foreach (var name in names)
        {
            Response.Write(name.Value);
        }


If you want to Load from XML file then you can follow below snippetes.

XmlDocument doc = new XmlDocument();
        doc.Load(@"c:\hiren.xml");
        XElement xdoc = XElement.Parse(doc.OuterXml);


Please vote and Accept Answer if it Helped.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900