Click here to Skip to main content
14,768,210 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm really struggling with understanding namespaces and reading from an XML file. I can read "simple" data from the XML file, but as as soon as the data is "hiding" behind a new XSD/NameSpace my program raise an error: "

System.Xml.XPath.XPathException: 'Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.'"

//READ THE XML FILE AND TAKE ACTIONS
XmlDocument serverDoc = new XmlDocument();
serverDoc.Load(fileName);
var nsmgr = new XmlNamespaceManager(serverDoc.NameTable);

nsmgr.AddNamespace("catalogue_item_notification", "urn:gs1:gdsn:catalogue_item_notification:xsd:3");
nsmgr.AddNamespace("allergen_information", "urn:gs1:gdsn:allergen_information:xsd:3");

XmlNodeList xml = serverDoc.SelectNodes("/catalogue_item_notification:catalogueItemNotificationMessage/transaction/documentCommand/catalogue_item_notification:catalogueItemNotification/catalogueItem", nsmgr);

foreach(XmlNode node in xml)
{

    //DECIDE NATURE OF UPDATE
    if (node.SelectSingleNode("tradeItem/tradeItemUnitDescriptorCode").InnerText == "BASE_UNIT_OR_EACH")
    {
        var glncode = node.SelectSingleNode("tradeItem/gtin").InnerText;
        var partyName = node.SelectSingleNode("tradeItem/informationProviderOfTradeItem/partyName").InnerText;
        var allergen = node.SelectSingleNode("tradeItem/tradeItemInformation/extension/allergen_information:allergenInformationModule/allergenRelatedInformation/allergenSpecificationAgency").InnerText;  (THIS ONE IS THROWING THE ERROR)

        Console.WriteLine(fileName);
        Console.WriteLine(glncode);
        Console.WriteLine(partyName);
        Console.WriteLine(allergen);
        Console.ReadKey();

    }


My XML file is a GS1 "item notification" file. This is part of the file:

<pre><?xml version="1.0" encoding="utf-8"?>
<catalogue_item_notification:catalogueItemNotificationMessage xmlns:sh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:gs1:gdsn:catalogue_item_notification:xsd:3 http://www.gdsregistry.org/3.1/schemas/gs1/gdsn/CatalogueItemNotification.xsd" xmlns:catalogue_item_notification="urn:gs1:gdsn:catalogue_item_notification:xsd:3">
  <sh:StandardBusinessDocumentHeader>
    <sh:HeaderVersion>1.0</sh:HeaderVersion>
    <sh:Sender>
      <sh:Identifier Authority="GS1">5790000500000</sh:Identifier>
    </sh:Sender>
    <sh:Receiver>
      <sh:Identifier Authority="GS1">5790000718689</sh:Identifier>
    </sh:Receiver>
    <sh:DocumentIdentification>
      <sh:Standard>GS1</sh:Standard>
      <sh:TypeVersion>3.1</sh:TypeVersion>
      <sh:InstanceIdentifier>Message-b7075b70-6739-4bc8-978a-9e9aed65f858</sh:InstanceIdentifier>
      <sh:Type>catalogueItemNotification</sh:Type>
      <sh:CreationDateAndTime>2020-11-03T11:06:54.8395136+00:00</sh:CreationDateAndTime>
    </sh:DocumentIdentification>
  </sh:StandardBusinessDocumentHeader>
  <transaction>
    <transactionIdentification>
      <entityIdentification>Transaction-617506c2-6c9b-4cab-82e8-2fc53a168c47</entityIdentification>
      <contentOwner>
        <gln>5790001104061</gln>
      </contentOwner>
    </transactionIdentification>
    <documentCommand>
      <documentCommandHeader type="CHANGE_BY_REFRESH">
        <documentCommandIdentification>
          <entityIdentification>DocumentCommand-7928fe5f-421f-4046-8629-9e1483183eb8</entityIdentification>
          <contentOwner>
            <gln>5790001104061</gln>
          </contentOwner>
        </documentCommandIdentification>
      </documentCommandHeader>


I can read all of the above - no issues... But further down in the XML file this comes up and trying to read from this, throw's the error (f.x.) trying to read the "allergenSpecificationAgency":

<tradeItemInformation>
           <extension>
             <allergen_information:allergenInformationModule xsi:schemaLocation="urn:gs1:gdsn:allergen_information:xsd:3 http://www.gdsregistry.org/3.1/schemas/gs1/gdsn/AllergenInformationModule.xsd" xmlns:allergen_information="urn:gs1:gdsn:allergen_information:xsd:3">
               <allergenRelatedInformation>
                 <allergenSpecificationAgency>EU</allergenSpecificationAgency>
                 <allergenSpecificationName>2003/89/EC</allergenSpecificationName>
                 <allergen>
                   <allergenTypeCode>AW</allergenTypeCode>
                   <levelOfContainmentCode>UNDECLARED</levelOfContainmentCode>
                 </allergen>
                 <allergen>
                   <allergenTypeCode>AC</allergenTypeCode>
                   <levelOfContainmentCode>UNDECLARED</levelOfContainmentCode>
                 </allergen>


What I have tried:

I have really tried all kinds of AddNamespace, but it doesn't seem to work.

Thanks for any help - highly appreciated :)
Posted
Updated 4-Nov-20 5:17am

Try passing the namespace manager to the SelectSingleNode method:
var allergen = node.SelectSingleNode("tradeItem/tradeItemInformation/extension/allergen_information:allergenInformationModule/allergenRelatedInformation/allergenSpecificationAgency", nsmgr).InnerText;
XmlNode.SelectSingleNode Method (System.Xml) | Microsoft Docs[^]
   
Comments
Member 14045539 4-Nov-20 10:17am
   
It worked! Absolutely fantastic! Thanks you very much!
Richards solution worked, by just adding the namespace reference to the end of the SelectSingleNode...
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900