Lucas_Santos
Lucas_Santos

Reputation: 4740

How can I do a LINQ to read XML file

I'm trying to read a XML file, but I want to know just the name of the node and her value.

Example

<User>
    <Contact>
        <Name>Lucas</Name>
        <ID>123</ID>
    </Contact>
</User>

I'm trying use this code below but without success. How can I return just the node name and her value ?

foreach (var name in doc2.Root.DescendantNodes().OfType<XNode>().Select(x => x).Distinct())
{
    string nome = name.ToString();
}

I want that my string receives the name of my Node, is this case will be Name and her value, that will be Lucas

Upvotes: 0

Views: 69

Answers (1)

SLaks
SLaks

Reputation: 887453

foreach(var element in doc2.Root.Descendants()) {
    String name = element.Name.LocalName;
    String value = element.Value;
}

Upvotes: 3

Related Questions