Reading a XML File, what am I doing wrong?

nuno.silva
Member Posts: 29
Hi,
I need to call a web service and read the XML.
I can call the web service, receive the reply but when I try to read the values, for example put the values on a message, I always get the error “A DotNet variable has not been instantiated. Attempting to call System.Xml.XmlNode.Value in CodeUnit CallWS: GetUsers”
Any sugestion?
thanks in advance,
NS
I need to call a web service and read the XML.
I can call the web service, receive the reply but when I try to read the values, for example put the values on a message, I always get the error “A DotNet variable has not been instantiated. Attempting to call System.Xml.XmlNode.Value in CodeUnit CallWS: GetUsers”
Any sugestion?
thanks in advance,
NS
===============
Nuno Silva
Nuno Silva
0
Best Answers
-
You need to change all reference from XMLNodexx.Value to XMLNodexx.InnerText
And you need to remove the namespaces too.
You can use something like thatRemoveNamespace(XMLDocIn : DotNet "System.Xml.XmlDocument";VAR XMLDocOut : DotNet "System.Xml.XmlDocument") // before this function your tag looks like this: <namespace:tagname>sample text</tagname> // after this function your tag looks like this: <tagname>sample text</tagname> // additionally all namespace references in the head are removed. XslTransform := XslTransform.XslTransform; XMLStyleSheet := XMLStyleSheet.XmlDocument; XMLStyleSheet.InnerXml( '<?xml version="1.0" encoding="UTF-8"?>' + '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">' + '<xsl:output method="xml" encoding="UTF-8" />' + '<xsl:template match="/">' + '<xsl:copy>' + '<xsl:apply-templates />' + '</xsl:copy>' + '</xsl:template>' + '<xsl:template match="*">' + '<xsl:element name="{local-name()}">' + '<xsl:apply-templates select="@* | node()" />' + '</xsl:element>' + '</xsl:template>' + '<xsl:template match="@*">' + '<xsl:attribute name="{local-name()}"><xsl:value-of select="."/></xsl:attribute>' + '</xsl:template>' + '<xsl:template match="text() | processing-instruction() | comment()">' + '<xsl:copy />' + '</xsl:template>' + '</xsl:stylesheet>' ); XslTransform.Load(XMLStyleSheet); writer := writer.StringWriter(); XslTransform.Transform(XMLDocIn, nullXsltArgumentList, writer); XMLDocOut := XMLDocOut.XmlDocument; // Inicializa el XML de salida XMLDocOut.InnerXml(writer.ToString());
Local vars
Name DataType Subtype Length
XMLStyleSheet DotNet System.Xml.XmlDocument.'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
XslTransform DotNet System.Xml.Xsl.XslTransform.'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
writer DotNet System.IO.StringWriter.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
nullXsltArgumentList DotNet System.Xml.Xsl.XsltArgumentList.'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
And put this code:RemoveNamespace(XMLResponseDoc, XMLResponseDoc);
before this line:XMLNodeList := XMLResponseDoc.GetElementsByTagName('Utilizador');
5 -
Sorry another thing to do is delete the /text() part in SelectSingleNode:
XMLNodeID := XMLNodeContainer.SelectSingleNode('Id'); XMLNodeUserName := XMLNodeContainer.SelectSingleNode('Username'); XMLNodeFullName := XMLNodeContainer.SelectSingleNode('FullName');
5
Answers
-
-
at the message line===============
Nuno Silva0 -
Maybe this works?
XMLNodeList := XMLResponseDoc.GetElementsByTagName('Utilizador'); FOR I := 0 TO XMLNodeList.Count - 1 DO BEGIN XMLNodeContainer := XMLNodeList.Item(I); XMLNodeID := XMLNodeContainer.SelectSingleNode('Id/text()'); XMLNodeUserName := XMLNodeContainer.SelectSingleNode('Username/text()'); XMLNodeFullName := XMLNodeContainer.SelectSingleNode('FullName/text()'); MESSAGE('%1 - %2 - %3', FORMAT(XMLNodeID.Value), FORMAT(XMLNodeUserName.Value), FORMAT(XMLNodeFullName.Value)); END;
My blog - https://www.HannesHolst.com/0 -
The XMLNodeContainer is the type of System.Xml.XmlNode, correct?
I get the same error.
If I put the code below, ir runs through the records returned by the web service
IF NOT ISNULL(XMLNodeID) THEN
MESSAGE('%1 - %2 - %3', FORMAT(XMLNodeID.Value), FORMAT(XMLNodeUserName.Value), FORMAT(XMLNodeFullName.Value));===============
Nuno Silva0 -
You need to change all reference from XMLNodexx.Value to XMLNodexx.InnerText
And you need to remove the namespaces too.
You can use something like thatRemoveNamespace(XMLDocIn : DotNet "System.Xml.XmlDocument";VAR XMLDocOut : DotNet "System.Xml.XmlDocument") // before this function your tag looks like this: <namespace:tagname>sample text</tagname> // after this function your tag looks like this: <tagname>sample text</tagname> // additionally all namespace references in the head are removed. XslTransform := XslTransform.XslTransform; XMLStyleSheet := XMLStyleSheet.XmlDocument; XMLStyleSheet.InnerXml( '<?xml version="1.0" encoding="UTF-8"?>' + '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">' + '<xsl:output method="xml" encoding="UTF-8" />' + '<xsl:template match="/">' + '<xsl:copy>' + '<xsl:apply-templates />' + '</xsl:copy>' + '</xsl:template>' + '<xsl:template match="*">' + '<xsl:element name="{local-name()}">' + '<xsl:apply-templates select="@* | node()" />' + '</xsl:element>' + '</xsl:template>' + '<xsl:template match="@*">' + '<xsl:attribute name="{local-name()}"><xsl:value-of select="."/></xsl:attribute>' + '</xsl:template>' + '<xsl:template match="text() | processing-instruction() | comment()">' + '<xsl:copy />' + '</xsl:template>' + '</xsl:stylesheet>' ); XslTransform.Load(XMLStyleSheet); writer := writer.StringWriter(); XslTransform.Transform(XMLDocIn, nullXsltArgumentList, writer); XMLDocOut := XMLDocOut.XmlDocument; // Inicializa el XML de salida XMLDocOut.InnerXml(writer.ToString());
Local vars
Name DataType Subtype Length
XMLStyleSheet DotNet System.Xml.XmlDocument.'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
XslTransform DotNet System.Xml.Xsl.XslTransform.'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
writer DotNet System.IO.StringWriter.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
nullXsltArgumentList DotNet System.Xml.Xsl.XsltArgumentList.'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
And put this code:RemoveNamespace(XMLResponseDoc, XMLResponseDoc);
before this line:XMLNodeList := XMLResponseDoc.GetElementsByTagName('Utilizador');
5 -
Sorry another thing to do is delete the /text() part in SelectSingleNode:
XMLNodeID := XMLNodeContainer.SelectSingleNode('Id'); XMLNodeUserName := XMLNodeContainer.SelectSingleNode('Username'); XMLNodeFullName := XMLNodeContainer.SelectSingleNode('FullName');
5 -
I already founded a solution below, but I think yours is best, so I replaced my solution by yours.
Thanks @ftorneroXMLNodeListRead := XMLResponseDoc.GetElementsByTagName('Id'); XMLNodeID := XMLNodeListRead.Item(I); XMLNodeListRead := XMLResponseDoc.GetElementsByTagName('Username'); XMLNodeUserName := XMLNodeListRead.Item(I); XMLNodeListRead := XMLResponseDoc.GetElementsByTagName('FullName'); XMLNodeFullName := XMLNodeListRead.Item(I);
===============
Nuno Silva0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions