How can I get the index of my current XML tag ?
Example:
<User>
<Contact>
<Name>Lucas</Name>
</Contact>
<Contact>
<Name>Andre</Name>
</Contact>
...
</User>
I’m trying the code below
foreach (var element2 in doc2.Root.Descendants())
{
String name = element.Name.LocalName;
String value = element.Value;
}
I want to know if I’m reading the first <Contact> tag, or the second, or the third…
Using the appropriate overload of Select will yield the index as you enumerate the collection.
Note: I added the .Where because .Descendants will recurse.