0

Following is the example of an XML document.

<People>
   <Person>
       <Name>ABC </Name>
       <SSN>111111</SSN>
       <Address>asdfg</Address>
   </Person>
</People>

I need to get the tag names but not the values between the tag names. That is, under the person tag, I should grab the Name, SSN, and Address nodes and not the ABC, 111111, and asdfg values.

I need to use LINQ to XML and query it in C#.

How can I do it?

1
  • Some reason my XML document example is not shown correctly above. Basicaly. It has one main tag People. Under which Ihave Person Tag. Which has Name, SSN and Address tags (each one has values). I need to get the tag names and not the values Commented Oct 1, 2009 at 18:41

4 Answers 4

4

This returns the names as a list of strings:

var doc = XDocument.Parse(@"<People>
   <Person>
       <Name>ABC </Name>
       <SSN>111111</SSN>
       <Address>asdfg</Address>
   </Person>
</People>"
);

var list = doc.Root.Element("Person").Descendants()
              .Select(node => node.Name.LocalName).ToList();

In case you're using an XElement instead of an XDocument, you can remove the .Root from the above code and get the correct results.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for being the only answer so far to actually use LINQ with the LINQ to XML classes.
1

Create a class

public class Person
{
     public string Name {get; set;}
     public int SSN {get; set;}
     public string Address {get; set;}
}

And create a new person this way;

List<Person> NewPersons = new List<Person>();
XDocument doc = XDocument.Load(xmlFile);     
foreach(XElement xElem in doc.Descendants("Person"))
{
    NewPersons.Add(new Person
            {
                Name = xElem. Element("Name").Value,
                SSN = xElem.Element("SSN").Value,
                Address = xElem.Element("Address").Value,
            });
}

Comments

0

You can get it this way...

string xml = "<People> <Person> <Name>ABC </Name> <SSN>111111</SSN> <Address>asdfg</Address> </Person> <Person> <Name>ABC </Name> <SSN>111111</SSN> <Address>asdfg</Address> </Person> </People>";

XElement xmlData = XElement.Parse(xml);

foreach(XElement xmlPerson in xmlData.Elements("Person"))
{
    List<string> TagsForThisPerson = new List<string>();
    foreach(XElement xmlTag in xmlPerson.Elements())
    {
        TagsForThisPerson.Add(xmlTag.Name.ToString());
    }
    TagsForThisPerson.Dump();
}

Comments

0

Simple LINQ syntax to get the names is listed below. It assumes you have the XML loaded in a XDocument variable named doc.

var nodeNames = from node in doc.Descendants("Person").First().Descendants()
                select node.Name.LocalName;

It only looks at the first person. If you have more than one in the XML document, the list of names is probably not what you would want (no reason to repeat all the names of the nodes over and over). This way, you get a list of just the node names for the first person, but it does assume that the first one would have a complete list of names. If they vary, you would need to build a distinct list from all the nodes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.