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.'"
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)
{
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>="1.0"="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 :)