MSXML2 to System.Xml
-
Wednesday, May 19, 2010 12:51 PM
Hi I am converting the VB code to vb.net. I have to remove the refrence to MSXML2.dll and use the dot net System.XMl name space.
The dot net intellisense gives suggestions on what should be the replaceable code for MSXML2.DOMDocument that is System.Xml.XMLDocument but it says 'No correction suggestions' for the MSXML2.XSLTemplate30. I want to know should I replace it with System.Xml.Xsl.XslTransform .
Also , how can we convert this code
xslDoc.parseError.errorCode,
xslDoc.parseError.reason
Or if anyone could please have a look at the following code and suggest how to convert it and can there be any issues at run time ??
Public Function convertDefaultXMLToXML(ByVal fileName As String, ByVal XmlDom As MSXML2.DOMDocument, Optional ByRef xslParameters As String = "") As MSXML2.DOMDocument
Dim xmlDoc As New XmlDocument
Dim xslt As New MSXML2.XSLTemplate30
Dim xslDoc As New MSXML2.FreeThreadedDOMDocument
Dim xslProc As MSXML2.IXSLProcessor
Try
Dim sLogPath As String
xslDoc.async = False
xslDoc.load(m_sXSLPath & fileName & ".xsl")
Dim tempArray() As String
Dim xslParameter() As String
Dim x As Integer
If (xslDoc.parseError.errorCode = 0) Then
xslt.stylesheet = xslDoc
xslProc = xslt.createProcessor()
xslProc.input = XmlDom
xslProc.output = xmlDoc
If Trim(xslParameters) <> "" Then
If InStr(1, xslParameters, "|") > 0 Then
tempArray = Split(xslParameters,
"|")
For x = 0 To UBound(tempArray)
xslParameter = Split(tempArray(x),
"=")
xslProc.addParameter(xslParameter(0), xslParameter(1))
Next
Else
xslParameter = Split(xslParameters,
"=")
xslProc.addParameter(xslParameter(0), xslParameter(1))
End If
End If
xslProc.transform()
Else
xmlDoc = setErrorXML(
"", xslDoc.parseError.errorCode, xslDoc.parseError.reason)
End If
convertDefaultXMLToXML = xmlDoc
Catch Ex As Exception
xmlDoc = setErrorXML("", ErrNumber(Ex), Ex.Message)
convertDefaultXMLToXML = xmlDoc
Finally
'Destroy Objects
xslt =
Nothing
xslDoc =
Nothing
xmlDoc =
Nothing
xslProc =
Nothing
End Try
- Moved by SamAgain Thursday, May 20, 2010 6:17 AM betterfit (From:.NET Base Class Library)
- Moved by Qi Samuel ZhangMicrosoft Employee, Moderator Tuesday, February 22, 2011 4:26 AM (From:XML in Windows (MSXML and XmlLite))
All Replies
-
Wednesday, May 19, 2010 1:37 PM
The similar functions are not available in vb.net. I have rewritten the code like this
Dim objXMLReader As XmlReader Dim objProcessor As Xsl.XslCompiledTransform Dim objArguList As Xsl.XsltArgumentList Dim objStream As IO.Stream 'strXSLToApply contains the xsl in string format. you can pass the file name also using overloaded method objXMLReader = XmlReader.Create(New System.IO.StringReader(strXSLToApply)) objProcessor = New Xsl.XslCompiledTransform objProcessor.Load(objXMLReader) objArguList = New Xsl.XsltArgumentList objArguList.AddParam("Parametername", "", "paramvalue") objStream = New IO.MemoryStream 'objXMLDOMDocument - contains the xml to be transformed objProcessor.Transform(objXMLDOMDocument, objArguList, objStream) 'strSQL contains the transformed output If objStream.Length > 3 Then Dim objByte(objStream.Length - 4) As Byte 'First three bytes contains UTF-8 BOM characters which should not be decoded objStream.Position = 3 objStream.Read(objByte, 0, objByte.Length) strSQL = System.Text.Encoding.UTF8.GetString(objByte) Else strSQL = "" End If
Jagatheesan- Marked As Answer by Monic3a Thursday, May 20, 2010 1:38 PM
-
Thursday, May 20, 2010 1:43 PM
Thanks a lot Jagatheesan for replying quickly. This code sure helped me a lot.
However, Is there any way I can get these values
xslDoc.parseError.errorCode
xslDoc.parseError.reason
xslDoc.parseError.line
xslDoc.parseError.linepos
xslDoc.parseError.srcText
Thanks.
-
Thursday, May 20, 2010 1:58 PM
Load methods raises parse errors as xmlexception using which you can read these values
example
try
objxml.load(xmlstring)
catch ex as XmlException
linenumber = ex.linenumber
linepos = ex.lineposition
source = ex.source
end try
refer this link for complete list of xmlexception properties
http://msdn.microsoft.com/en-us/library/system.xml.xmlexception_members.aspx
hope this helps
Jagatheesan- Marked As Answer by Monic3a Friday, May 21, 2010 6:34 AM
-
Friday, May 21, 2010 6:31 AM
The link is very helpful and I am now getting a clearer picture of the things.
Thanks a lot for your help.
-
Tuesday, May 25, 2010 9:09 AM
Hi Jagatheesan,
I Implemented the above code but the problem is my xml document is transformed but it si not structured . Following is my xml file
XML File
<xml xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
<s:Schema id="RowsetSchema">
<s:ElementType name="row" content="eltOnly">
<s:AttributeType name="HCR_ID" rs:number="1">
<s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="20" rs:fixedlength="true" rs:maybenull="false"/>
</s:AttributeType>
<s:AttributeType name="LOTUS_NOTES_ID" rs:number="2">
<s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="10" rs:fixedlength="true" rs:maybenull="false"/>
</s:AttributeType>
<s:AttributeType name="ACTIVE_IND" rs:number="3">
<s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="1" rs:fixedlength="true" rs:maybenull="false"/>
</s:AttributeType>
<s:AttributeType name="HCR_ROLE_ID" rs:number="4">
<s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="4" rs:fixedlength="true" rs:maybenull="false"/>
</s:AttributeType>
<s:AttributeType name="ASSIGNMENT" rs:number="5" rs:nullable="true">
<s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="1" rs:fixedlength="true"/>
</s:AttributeType>
<s:AttributeType name="LAST_NM" rs:number="6" rs:nullable="true">
<s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="50" rs:fixedlength="true"/>
</s:AttributeType>
<s:AttributeType name="FIRST_NM" rs:number="7" rs:nullable="true">
<s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="50" rs:fixedlength="true"/>
</s:AttributeType>
<s:extends type="rs:rowbase"/>
</s:ElementType>
</s:Schema>
<rs:data>
<z:row HCR_ID="67822181714161206002" LOTUS_NOTES_ID="LCADMN1 " ACTIVE_IND="Y" HCR_ROLE_ID="30 " ASSIGNMENT="Y" LAST_NM="AdminOne " FIRST_NM="LC "/>
</rs:data>
</xml>Here is the XSL file
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:z="#RowsetSchema" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" exclude-result-prefixes="s z rs dt">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:template match="/">
<RESPONSE40>
<FIELDS40>
<ROW40>
<xsl:apply-templates select="xml/rs:data/z:row"/>
</ROW40>
</FIELDS40>
</RESPONSE40>
</xsl:template>
<xsl:template match="z:row">
<ROLEID>
<xsl:value-of select="normalize-space(@HCR_ROLE_ID)"/>
</ROLEID>
<ID>
<xsl:value-of select="normalize-space(@HCR_ID)"/>
</ID>
<FIRSTNAME>
<xsl:value-of select="normalize-space(@FIRST_NM)"/>
</FIRSTNAME>
<LASTNAME>
<xsl:value-of select="normalize-space(@LAST_NM)"/>
</LASTNAME>
<MIDDLENAME>
<xsl:value-of select="normalize-space(@MIDDLE_NM)"/>
</MIDDLENAME>
<ISACTIVE>
<xsl:value-of select="normalize-space(@ACTIVE_IND)"/>
</ISACTIVE>
<NETWORKID>
<xsl:value-of select="normalize-space(@LOTUS_NOTES_ID)"/>
</NETWORKID>
<PHONE>
<xsl:value-of select="normalize-space(@PHONE_NUM)"/>
</PHONE>
<CELLPHONE>
<xsl:value-of select="normalize-space(@CELL_PHONE_NUM)"/>
</CELLPHONE>
<PAGER>
<xsl:value-of select="normalize-space(@PAGER_NUM)"/>
</PAGER>
<MANAGERID>
<xsl:value-of select="normalize-space(@MGR_ID)"/>
</MANAGERID>
</xsl:template>
</xsl:stylesheet>the output XML document which I am expecting should be like this ( I get this output when I use the VB6 code with MSXML) . I have copied from the immediate window
"<?xml version=""1.0""?>
<RESPONSE40>
<FIELDS40>
<ROW40>
<ROLEID>30</ROLEID>
<ID>67822181714161206002</ID>
<FIRSTNAME>LC</FIRSTNAME>
<LASTNAME>AdminOne</LASTNAME>
<MIDDLENAME></MIDDLENAME>
<ISACTIVE>Y</ISACTIVE>
<NETWORKID>LCADMN1</NETWORKID>
<PHONE></PHONE>
<CELLPHONE></CELLPHONE>
<PAGER></PAGER>
<MANAGERID></MANAGERID>
</ROW40>
</FIELDS40>
</RESPONSE40>
"I extended the code provided by you by one line
Dim objOutXML As New System.Xml.XmlDocument
objOutXML.LoadXml(strSQL)
But when I use the dot net code to transform I am getting this output .
?objOutXML.OuterXml
"<?xml version="1.0" encoding="utf-8"?><RESPONSE40><FIELDS40><ROW40><ROLEID>30</ROLEID><ID>67822181714161206002</ID><FIRSTNAME>LC</FIRSTNAME><LASTNAME>AdminOne</LASTNAME><MIDDLENAME></MIDDLENAME><ISACTIVE>Y</ISACTIVE><NETWORKID>LCADMN1</NETWORKID><PHONE></PHONE><CELLPHONE></CELLPHONE><PAGER></PAGER><MANAGERID></MANAGERID></ROW40></FIELDS40></RESPONSE40>"
The output is not structured
Could you please let me know why is this happening ? Or it is ok.
-
Tuesday, May 25, 2010 2:40 PM
Thats should be fine when you access your xml in the code, it does not impact. If you want to write the content into some file you can format it using xmlwriter settings. Refer this link for details
http://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.aspx
Jagatheesan -
Wednesday, May 26, 2010 10:26 AM
There is one more thing for which I am not getting any clear answres on google ..
what is the alternative of the serProperty
This is my code
objXMLDOM.setProperty(
"SelectionNamespaces", "xmlns:s=""uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"" xmlns:dt=""uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"" xmlns:rs=""urn:schemas-microsoft-com:rowset"" xmlns:z=""#RowsetSchema""")
Thanks
-
Wednesday, May 26, 2010 12:14 PMThese are the second level xml properties. i can't find alternative for this in system.xml. but if you give me the details of how exactly it is used and for what purpose these are used in your application I can provide the alternate.
Jagatheesan -
Wednesday, May 26, 2010 2:05 PM
I am using this to get the count of elements belonging to a particular namespace
My VB function is
Public
Function MGetCount(ByRef objXML As MSXML2.DOMDocument) As Short
On Error GoTo HandleError
objXML.setProperty(
"SelectionNamespaces", "xmlns:rs=""urn:schemas-microsoft-com:rowset"" xmlns:z=""#RowsetSchema""")
MGetCount =
CShort(objXML.selectNodes("xml/rs:data/z:row").length)
Exit Function
HandleError:
On Error Resume Next
MGetCount = 0
End Function
Th above code I have rewritten in dot net like this
Public
Function GetCount(ByRef objXML As System.Xml.XmlDocument) As Short
Dim ns As System.Xml.XmlNamespaceManager = New System.Xml.XmlNamespaceManager(objXML.NameTable)
'ns.AddNamespace("SelectionLanguage", "XPath")
ns.AddNamespace(
"SelectionNamespaces", "xmlns:rs=""urn:schemas-microsoft-com:rowset"" xmlns:z=""#RowsetSchema""")
Try
GetCount =
CShort(objXML.SelectNodes("xml/rs:data/z:row", ns).Count)
Catch ex As XPathException
GetCount = 0
End Try
End Function
-
Wednesday, May 26, 2010 2:07 PM
The XMl that I am using as Input is as follows
<xml xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010AA00C14882"
xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
<s:Schema id="RowsetSchema">
<s:ElementType name="row" content="eltOnly">
<s:AttributeType name="HCR_ID" rs:number="1">
<s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="20" rs:fixedlength="true" rs:maybenull="false"/>
</s:AttributeType>
<s:AttributeType name="LOTUS_NOTES_ID" rs:number="2">
<s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="10" rs:fixedlength="true" rs:maybenull="false"/>
</s:AttributeType>
<s:AttributeType name="ACTIVE_IND" rs:number="3">
<s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="1" rs:fixedlength="true" rs:maybenull="false"/>
</s:AttributeType>
<s:AttributeType name="HCR_ROLE_ID" rs:number="4">
<s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="4" rs:fixedlength="true" rs:maybenull="false"/>
</s:AttributeType>
<s:AttributeType name="ASSIGNMENT" rs:number="5" rs:nullable="true">
<s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="1" rs:fixedlength="true"/>
</s:AttributeType>
<s:AttributeType name="LAST_NM" rs:number="6" rs:nullable="true">
<s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="50" rs:fixedlength="true"/>
</s:AttributeType>
<s:AttributeType name="FIRST_NM" rs:number="7" rs:nullable="true">
<s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="50" rs:fixedlength="true"/>
</s:AttributeType>
<s:extends type="rs:rowbase"/>
</s:ElementType>
</s:Schema>
<rs:data>
<z:row HCR_ID="03944925513171206002" LOTUS_NOTES_ID="TESTLCR2 " ACTIVE_IND="Y" HCR_ROLE_ID="10 " ASSIGNMENT="Y"LAST_NM="Lcrtwo " FIRST_NM="Test "/>
<z:row HCR_ID="05628012156191906002" LOTUS_NOTES_ID="LROMGR1 " ACTIVE_IND="Y" HCR_ROLE_ID="25 " ASSIGNMENT="Y"LAST_NM="MgrOne " FIRST_NM="LRO "/>
<z:row HCR_ID="05705050033152208002" LOTUS_NOTES_ID="HILL " ACTIVE_IND="Y" HCR_ROLE_ID="10 " ASSIGNMENT="Y"LAST_NM="Gray{}|":?><()* " FIRST_NM="Orange%
"/>
<z:row HCR_ID="06962673013161409002" LOTUS_NOTES_ID="GREENC27 " ACTIVE_IND="N" HCR_ROLE_ID="10 " ASSIGNMENT="Y"LAST_NM="Rep II " FIRST_NM="Testing "/>
</rs:data>
</xml>The VB version works fine and gives me the count as 4.
But when I run the dot net version is throws an exception saying
Namespace prefix 'z' is not defined
Could you please suggest something .
I appreaciate all your help.
Thanks
-
Wednesday, May 26, 2010 2:27 PM
Hi Jagatheesan,
I changed the dot net version of the function as follows now the error has gone but the value of GetCount is coming as 0 (Not 4)
Public
Function GetCount(ByRef objXML As System.Xml.XmlDocument) As Short
Dim ns As System.Xml.XmlNamespaceManager = New System.Xml.XmlNamespaceManager(objXML.NameTable)
ns.AddNamespace(
"rs", "xmlns:rs=""urn:schemas-microsoft-com:rowset""")
ns.AddNamespace(
"z", "xmlns:z=""#RowsetSchema""")
Try
GetCount =
CShort(objXML.SelectNodes("xml/rs:data/z:row", ns).Count)
Catch ex As XPathException
GetCount = 0
End Try
End Function
Thanks.
-
Wednesday, May 26, 2010 3:16 PM
There is one more thing for which I am not getting any clear answres on google ..
what is the alternative of the serProperty
This is my code
objXMLDOM.setProperty(
"SelectionNamespaces" , "xmlns:s=""uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"" xmlns:dt=""uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"" xmlns:rs=""urn:schemas-microsoft-com:rowset"" xmlns:z=""#RowsetSchema""" )
Thanks
Use an XmlNamespaceManager e.g.
XmlNamespaceManager mgr = new XmlNamespaceManager(xmlDocumentInstance.NameTable);
mgr.AddNamespace("s", "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882");
// add the other namespaces here
// then pass the XmlNamespaceManager object "mgr" to any SelectNodes and SelectSingleNode calls as the second argument
MVP Data Platform Development My blog -
Thursday, May 27, 2010 5:51 AM
Corrected code below. In namespace parameters just give the namespace without prefix
ns.AddNamespace( "rs" , "urn:schemas-microsoft-com:rowset" )ns.AddNamespace( "z" , "#RowsetSchema" )
Jagatheesan- Marked As Answer by Monic3a Thursday, May 27, 2010 5:55 AM
-
Thursday, May 27, 2010 5:56 AMOh Yes. It worked. Thanks a lot.
-
Tuesday, June 08, 2010 12:24 PM
Hi Jagatheesan,
I got a couple of more challenges while doing this migration from MSXML2 to System.XML
The equivalent of Node.text in MSXL2 is the "InnerText" of System.XML . When I see the output of the two in immediate window this is what I notice (I am using Books.xml from msdn articles)
?childNode.InnerText
"Blue Smoke19.95"
?childNode1.text
"Blue Smoke 19.95"
?childNode1.text.Length
16
?childNode.InnerText.Length
15
As you can see there is no space in between the Book name and price in the dot net version . Can we call this a Bug ? What can be the workaroud for this .
One more thing is there, as you can see down from the immediate window output. I printed the XML of the childnode . In the MSXML2 version the output is properly indented but in the dot net version (outerxml) the output is displayed as a single line .
What can we do for this. This may not be an issue at runtime but still why is this behaviour there ? How can I get the indented output ?
?childNode.OuterXml
"<book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-8" xmlns:bk="urn:samples"><title>Blue Smoke</title><price>19.95</price></book>"
?childNode1.xml
"<book genre="novel" publicationdate="1991" xmlns:bk="urn:samples" bk:ISBN="1-861001-57-8">
<title>Blue Smoke</title>
<price>19.95</price>
</book>"
Thanks a lot for all you help once again.
-
Tuesday, June 08, 2010 12:31 PM
For the .NET System.Xml.XmlDocument instance you might want to set
Dim doc As New XmlDocument()
doc.PreserveWhitespace = True
before you load any data, that way the OuterXml will reflect the white space that was originally in the source XML.
If you don't want to set that property but still want intended output then don't use OuterXml, instead use the Save method, that will be default add indentation.
MVP Data Platform Development My blog -
Tuesday, June 08, 2010 1:05 PM
Ok. This is the difference between text and innertext methods. But normally we use innerText to find the value of single node. Why do you want to read the values of more than one node at a time? I can provide the workaround based on your need here.
Your next question also deals with difference between the parsers. But it wont impact anything. If you want to write into some file you can use xmlwriter settings which has better control over MSXML2.
Jagatheesan -
Monday, June 14, 2010 10:08 AM
Hi,
Is there any way to use the Save method of classic ADO recordset with the System.XML.XMLDocument.
As when I try to run the following code I am getting this error
objRst.Save(objXMLDOM, ADODB.PersistFormatEnum.adPersistXML)
"Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another."
I googled it and found this link helpful to the extent that it explains that System.XML.XMLDocument is com invisible but as a solution they asked to use the MSXML2 document. This can not be the solution in my case beacuse I have to System.XML only.
So is there any workaroung that I can save my recorset data into the System.XML.XmlDocument .
Thanks a lot for your help.
-
Monday, June 14, 2010 11:23 AM
Try this
objRst.Save("sample.xml", ADODB.PersistFormatEnum.adPersistXML)
objXMLDOM.load("sample.xml")
Jagatheesan

