0

I'm trying to select nodes in c# within a XML file with below example.

<dsEq xmlns="http://tempuri.org/dsEq.xsd">
   <Dago>
      <EID>XX</EID> 

below code is working :

private static List<string> getListOfEID(XmlDocument xmlDoc)
{
    List<string> ListingEID = new List<string>();
    XmlNodeList nodeCollection = xmlDoc.GetElementsByTagName("EID");

    foreach (XmlNode elt in nodeCollection)
    {
        ListingEID.Add(elt.InnerText.ToString());
    }
    return ListingEID;
}

While I tried a lot of things, without success with Selectnodes method, not working :

private static List<string> getListOfEID(XmlDocument xmlDoc)
{
    List<string> ListingEID = new List<string>();
    XmlNodeList nodeCollection = xmlDoc.SelectNodes("/dsEq/Dago/EID");

    foreach (XmlNode elt in nodeCollection)
    {
        ListingEID.Add(elt.InnerText.ToString());
    }
    return ListingEID;
}

Thanks in advance !

tried a lot of different xPath without success.

adding nameSpace seems not helping

    private static List<string> getListOfEID(XmlDocument xmlDoc)
    {
        List<string> ListingEID = new List<string>();

        XmlNamespaceManager nameManager = new XmlNamespaceManager(xmlDoc.NameTable);
        nameManager.AddNamespace("myEID","/dsEq/Dago");

        XmlNodeList nodeCollection = xmlDoc.SelectNodes("myEID");

        foreach (XmlNode elt in nodeCollection)
        {
            ListingEID.Add(elt.InnerText.ToString());
        }
        return ListingEID;
    }

==> Show nil node

ThibaultKm
  • 25
  • 5
  • 1
    Does this answer your question? [How do I select nodes that use a default namespace?](https://stackoverflow.com/questions/14370989/how-do-i-select-nodes-that-use-a-default-namespace) – Klaus Gütter Mar 24 '22 at 15:03
  • Do you *have* to use XmlDocument? LINQ to XML has *much* cleaner support for namespaces IMO. (As an aside, I'd strongly advise you to start following .NET naming conventions as early as possible.) – Jon Skeet Mar 24 '22 at 15:26
  • i could switch to LINQ, but i'm curious also why it isn't working with xPath, even if LINQ would be more efficient. Thanks for your recommandation regarding .NET conventions, i'll get more into it soon – ThibaultKm Mar 24 '22 at 15:30

2 Answers2

1

It is very easy via LINQ to XML.

LINQ to XML is available in the .Net Framework since 2007.

c#

void Main()
{
    XDocument xdoc = XDocument.Parse(@"<dsEq xmlns='http://tempuri.org/dsEq.xsd'>
            <Dago>
                <EID>XX</EID>
            </Dago>
            <Dago>
                <EID>YY</EID>
            </Dago>
        </dsEq>");
    
    XNamespace ns = xdoc.Root.GetDefaultNamespace();

    List<String> listOfStrings = xdoc.Descendants(ns + "EID")
      .Select(x => x.Value).ToList();
      
    Console.Write(listOfStrings);
}

enter image description here

Yitzhak Khabinsky
  • 18,471
  • 2
  • 15
  • 21
0

I too think it is super simple with Linq to XML as @Yitzhak Khabinsky showed already. Yours is not working because you are not using Xpath correctly.

void Main()
{
    var doc = new XmlDocument();
    doc.LoadXml(sample);
    var eids = getListOfEID(doc);
    foreach (var eid in eids)
    {
        Console.WriteLine(eid);
    }
}

private static List<string> getListOfEID(XmlDocument xmlDoc)
{
    List<string> ListingEID = new List<string>();

    XmlNamespaceManager nameManager = new XmlNamespaceManager(xmlDoc.NameTable);
    nameManager.AddNamespace("x","http://tempuri.org/dsEq.xsd");
    XmlNodeList nodeCollection = xmlDoc.SelectNodes("//x:EID", nameManager);

    foreach (XmlNode elt in nodeCollection)
    {
        ListingEID.Add(elt.InnerText.ToString());
    }
    return ListingEID;
}

static readonly string sample = @"<dsEq xmlns=""http://tempuri.org/dsEq.xsd"">
   <Dago>
      <EID>XX</EID>
    </Dago>
    <Dago>
      <EID>YY</EID>
    </Dago>
    <Dago>
      <EID>ZZ</EID>
    </Dago>
</dsEq>";
Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39
  • thank you ! I have then switched in LinQ since everybody recommended it. But will also take a look on how Xpath is working with you code. – ThibaultKm Mar 24 '22 at 18:34