0

I have an XML file looking like this:

<root xmlns="myNameSpace">
    <Director>
        <Name>Paul</Name>
    </Director>
    <Company>
        <Name>Sony</Name>
        <Identifier>8945</Identifier>
        <CreationDate>2016-01-19</CreationDate>
    </Company>
    <Signature xmlns="otherNS">
        <!-- Some datas-->
    </Signature>
</Root>

I'm coding a WCF WebService in C# and I want to parse the XML file. I want to check if the balise Company exist and if it contains the Name. WHat is the best way to check if a xmlNode contains a specific children ?

Here is my code :

private String writeResponse(XmlDocument xmlRequest)
{
    XmlNodeList CompagnyNode = xmlRequest.GetElementsByTagName("Company", Properties.Settings.Default.nameSpaceUri);
    if (CompagnyNode == null || CompagnyNode.Count <= 0)
        throw new ApplicationException("writeResponse : Missing Company");


    // Code below doesn't work

    // This part return an empty XmlNodeList (Count = 0)
    XmlNodeList test1 = xmlRequest.SelectNodes("/root/Company/Name");

    // This part return an empty XmlNodeList (Count = 0)
    XmlNodeList test2 = CompanyNode[0].SelectNodes("Name");

    // This part return too much node
    XmlNodeList test3 = xmlRequest.GetElementsByTagName("Name", Properties.Settings.Default.nameSpaceUri);
}
Bernard Vander Beken
  • 4,848
  • 5
  • 54
  • 76
A.Pissicat
  • 3,023
  • 4
  • 38
  • 93

0 Answers0