 |
| XML General XML discussions. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

May 17th, 2010, 07:18 AM
|
|
Registered User
|
|
Join Date: Apr 2005
Posts: 352
Thanks: 14
Thanked 0 Times in 0 Posts
|
|
classic ASP to ASP.NET 2.o XML coding
Hi,
What would be best converted code to .NET 2.0 for below classic ASP code?
Code:
Code:
Dim xmlDoc
Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
xmlDoc.resolveExternals = False
xmlDoc.validateOnParse = False
xmlDoc.preserveWhiteSpace = False
xmlDoc.loadXML(strResult) ' load the xml sent
If xmlDoc.parseError.errorCode = 0 Then
CODE = xmlDoc.selectSingleNode("/Root/Child1/SubCode").Text
INDICATOR = xmlDoc.selectSingleNode("/Root/Child2").Text
end if
Thank you.
|
|

May 17th, 2010, 07:24 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Along these lines, using the System.Xml namespace:
Code:
Imports System.Xml
...
Dim doc As New XmlDocument()
doc.XmlResolver = null
Try
doc.LoadXml(strResult)
Dim code As String = doc.SelectSingleNode("/Root/Child1/SubCode").InnerText
Dim indicator As String = doc.SelectSingleNode("/Root/Child2").InnerText
Catch ex As XmlException
' handle parse error exception here
End Try
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|

May 17th, 2010, 07:38 AM
|
|
Registered User
|
|
Join Date: Apr 2005
Posts: 352
Thanks: 14
Thanked 0 Times in 0 Posts
|
|
Thank you Martin,
I think it's now working, but gives error " Data at the root level is invalid. Line 1 position 1." for
LoadXml methond.
So, clearly XML file is invalid, please see below the content of XML file.:
<?xmlversion="1.0"encoding="utf-8"?>
<PrescriberLookupxmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns=http://www.xxx.com/webserv/>
<Licenses>
<License>
<State>TX</State>
<Number>TX12345</Number>
<ExpirationDate>
<FormatString>{0:G}</FormatString>
<Text>12/31/2020 12:00:00 AM</Text>
<Date>2020-12-31T00:00:00.0000000+05:30</Date>
</ExpirationDate>
</License>
</Licenses>
<Results>
<ReturnCode>0</ReturnCode>
<Descripton>Success</Descripton>
</Results>
<StatusIndicator>14</StatusIndicator>
<UserGuid>b603471e-51d3-4463-bff3-93fd99c7cd19</UserGuid>
<NameFirst>Test</NameFirst>
<NameLast>TestUser11</NameLast>
<NameMiddle />
<DEA>
<Number />
</DEA>
<ContactLocation>
<LocationName />
<Address1>9th Ave</Address1>
<Address2>Suite 312</Address2>
<Address3 />
<City>Houston</City>
<State>TX</State>
<PostalCode>77044</PostalCode>
<Phone>
<Number>281-555-1111</Number>
<Ext />
</Phone>
<Fax>
<Number>281-555-0011</Number>
<Ext />
</Fax>
</ContactLocation>
<UserSettings>
<StatusId>1</StatusId>
<IgnoreLastName>false</IgnoreLastName>
<ConfirmationReceived>true</ConfirmationReceived>
<PwdExpirationDate>2010-05-01T15:30:19.0000000+05:30</PwdExpirationDate>
<EOExpirationDate>2010-11-02T15:30:19.4200000+05:30</EOExpirationDate>
</UserSettings>
</PrescriberLookup>
Kindly guide..
Last edited by rupen; May 17th, 2010 at 07:39 AM..
Reason: security.
|
|

May 17th, 2010, 07:47 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Where do you get "strResult" from? Make sure it has no characters before the XML declaration or before the root element. Unfortunately the "Microsoft.XMDOM" object is rather sloppy when it comes to such issues and is not really compliant with the XML standard.
Note also that the System.Xml.XmlDocument has Load method that takes various types of arguments such as Stream or TextReader or XmlReader which might give better results then doing LoadXml with a string that you might have created with your own code from a Stream.
One additionally issue you will face is to deal with the default namespace declaration in your XML document, you will need to use an XmlNamespaceManager. But let's first resolve that problem.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|
The Following User Says Thank You to Martin Honnen For This Useful Post:
|
|
|

May 17th, 2010, 07:53 AM
|
|
Registered User
|
|
Join Date: Apr 2005
Posts: 352
Thanks: 14
Thanked 0 Times in 0 Posts
|
|
I modified the LoadXML as below:
xmlDoc.LoadXml(MapPath("TestData.xml"));
Where TestData.xml is in the same directory where the .aspx file is.
But still it is giving the same error.
|
|

May 17th, 2010, 07:56 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
MapPath("TestData.xml") returns a file path, but LoadXml is expecting a string of XML, not a filename.
The following will load the XML from within a file:
xmlDoc.Load(MapPath("TestData.xml"));
|
|

May 17th, 2010, 08:03 AM
|
|
Registered User
|
|
Join Date: Apr 2005
Posts: 352
Thanks: 14
Thanked 0 Times in 0 Posts
|
|
Ok..now using Load method: the error moves at code
Label1.Text = xmlDoc.SelectSingleNode("/PrescriberLookup/Results/ReturnCode").InnerText;
says : NullReferenceException was unhandle by user code: object reference not set to...
|
|

May 17th, 2010, 08:06 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
That will be the namespace issue that Martin mentioned.
You need to create an XmlNamespaceManager, and then use a prefix to specify the elements in your namespace:
Code:
XmlNamespaceManager mgr = new XmlNamespaceManager(xmlDoc.NameTable);
mgs.AddNamespace("my", "http://www.xxx.com/webserv/");
Label1.Text = xmlDoc.SelectSingleNode(/my:PrescriberLookup/my:Results/my:ReturnCode", mgr).InnerText;
|
|
The Following User Says Thank You to samjudson For This Useful Post:
|
|
|

May 17th, 2010, 08:15 AM
|
|
Registered User
|
|
Join Date: Apr 2005
Posts: 352
Thanks: 14
Thanked 0 Times in 0 Posts
|
|
Excellent!!! it is working fine.
Thank you, Martin and Sam.
|
|

May 18th, 2010, 02:27 AM
|
|
Registered User
|
|
Join Date: Apr 2005
Posts: 352
Thanks: 14
Thanked 0 Times in 0 Posts
|
|
Sorry to bother again!
Earlier we use xmlDoc.parseError.errorCod to check for error in Microsoft's XMLDOM, how we do the same in .NET 2.0?
|
|
 |