Traversing XMLFile using XMLNode DotNet Obj

jordi79jordi79 Member Posts: 275
edited 2014-05-06 in NAV Three Tier
Hi,
I am attempting to open an xml file and checking out the contents in the file. This works beautifully when the file has not xmlns name space, but once there is a xmlns name space definition, then I cannot traverse the xml file.

My code:
CLEARALL();
XMLDoc := XMLDoc.XmlDocument;


FileName := 'c:\temp\temp1.xml';
FileName := FileMgmt.UploadFileSilent(FileName);
IF NOT EXISTS(FileName) THEN EXIT;

f.OPEN(FileName);
f.TEXTMODE(TRUE);
f.CREATEINSTREAM(FileInStream);
XMLDoc.Load(FileInStream);

RootNode := XMLDoc.DocumentElement;
MESSAGE(RootNode.Name + '-' + RootNode.InnerText);

FindNode(RootNode, 'invoice', Node);
IF NOT ISNULL(Node) THEN BEGIN
  MESSAGE(Node.Name + '-' + Node.InnerText);  // this line is skipped if the xml file has xmlns, because the node cannot be found...
END;

The XML File with xmlns:
<content xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" 

xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ccts="urn:un:unece:uncefact:documentation:2" 

xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" 

xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<invoice>
		<id>inv_001</id>
		<name>user name</name>
	</invoice>
</content>

The xml file without xmlns:
<content>
	<invoice>
		<id>inv_001</id>
		<name>user name</name>
	</invoice>
</content>

Comments

  • SiStSiSt Member Posts: 46
    The problem is the SelectSingleNode function used by the FindNode function. There are examples from Microsoft how to remove the namespaces prior to parsing the file. You can also call the function yourself and use a proper namespace manager and XPath or use other functions of the dll to get the xml node.

    You can find a lot information if you google for XML namespaces and SelectSingleNode and an example how to handle it in the MSDN: http://msdn.microsoft.com/de-de/library/system.xml.xmlnode.selectsinglenode(v=vs.110).aspx

    The automation/dll itself is fully capable of handling namespaces and we do support it in Anveo EDI Connect.
Sign In or Register to comment.