-2

I have this xml:

<data_item>
    <warehouses>
        <wa_1>10</wa_1>
        <wa_2>6</wa_2>
    </warehouses>
</data_item>

I need to get the nodeName of the childNode.

Output:
wa_1
wa_2

Currently, i have these following codes but I get 'warehouses':

var warehouseElem = lineItemElem.Elements("warehouses");
var node = warehouseElem.FirstOrDefault();
var nodeName = node.Name;
Jen143
  • 815
  • 4
  • 17
  • 42
  • 3
    Okay, so there are *lots* of tutorials, questions etc about parsing XML. I'd suggest using LINQ to XML. Have you tried that yet? (e.g. start with `XDocument.Load` / `XDocument.Parse` and go from there). Currently, this question shows a lack of research. – Jon Skeet Apr 04 '17 at 07:34
  • Possible duplicate of [LINQ to read XML](http://stackoverflow.com/questions/670563/linq-to-read-xml) – ckruczek Apr 04 '17 at 07:39
  • Use node.Name.LocalName which returns a string. – jdweng Apr 04 '17 at 09:57

1 Answers1

2

"Currently, i have these following codes but I get 'warehouses'..."

You can use Elements() i.e without parameter to get all child elements from parent, warehouses, and then extract the element names from there :

var nodeNames = node.Elements().Select(n => n.Name);
foreach(var nodeName in nodeNames)
{
    Console.WriteLine(nodeName);
}
har07
  • 88,338
  • 12
  • 84
  • 137