Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XML
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 17th, 2010, 07:18 AM
Registered User
Points: 1,897, Level: 17
Points: 1,897, Level: 17 Points: 1,897, Level: 17 Points: 1,897, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2005
Location: Mumbai, Maharastra, India.
Posts: 352
Thanks: 14
Thanked 0 Times in 0 Posts
Send a message via Yahoo to rupen
Default 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.
Reply With Quote
  #2 (permalink)  
Old May 17th, 2010, 07:24 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

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
Reply With Quote
  #3 (permalink)  
Old May 17th, 2010, 07:38 AM
Registered User
Points: 1,897, Level: 17
Points: 1,897, Level: 17 Points: 1,897, Level: 17 Points: 1,897, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2005
Location: Mumbai, Maharastra, India.
Posts: 352
Thanks: 14
Thanked 0 Times in 0 Posts
Send a message via Yahoo to rupen
Default

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.
Reply With Quote
  #4 (permalink)  
Old May 17th, 2010, 07:47 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

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
Reply With Quote
The Following User Says Thank You to Martin Honnen For This Useful Post:
rupen (May 17th, 2010)
  #5 (permalink)  
Old May 17th, 2010, 07:53 AM
Registered User
Points: 1,897, Level: 17
Points: 1,897, Level: 17 Points: 1,897, Level: 17 Points: 1,897, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2005
Location: Mumbai, Maharastra, India.
Posts: 352
Thanks: 14
Thanked 0 Times in 0 Posts
Send a message via Yahoo to rupen
Default

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.
Reply With Quote
  #6 (permalink)  
Old May 17th, 2010, 07:56 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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"));
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
  #7 (permalink)  
Old May 17th, 2010, 08:03 AM
Registered User
Points: 1,897, Level: 17
Points: 1,897, Level: 17 Points: 1,897, Level: 17 Points: 1,897, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2005
Location: Mumbai, Maharastra, India.
Posts: 352
Thanks: 14
Thanked 0 Times in 0 Posts
Send a message via Yahoo to rupen
Default

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...
Reply With Quote
  #8 (permalink)  
Old May 17th, 2010, 08:06 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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;
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
The Following User Says Thank You to samjudson For This Useful Post:
rupen (May 17th, 2010)
  #9 (permalink)  
Old May 17th, 2010, 08:15 AM
Registered User
Points: 1,897, Level: 17
Points: 1,897, Level: 17 Points: 1,897, Level: 17 Points: 1,897, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2005
Location: Mumbai, Maharastra, India.
Posts: 352
Thanks: 14
Thanked 0 Times in 0 Posts
Send a message via Yahoo to rupen
Default

Excellent!!! it is working fine.

Thank you, Martin and Sam.
Reply With Quote
  #10 (permalink)  
Old May 18th, 2010, 02:27 AM
Registered User
Points: 1,897, Level: 17
Points: 1,897, Level: 17 Points: 1,897, Level: 17 Points: 1,897, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2005
Location: Mumbai, Maharastra, India.
Posts: 352
Thanks: 14
Thanked 0 Times in 0 Posts
Send a message via Yahoo to rupen
Default

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?
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
classic ASP to ASP.NET 2.o XML coding rupen ASP.NET 2.0 Basics 0 May 14th, 2010 07:25 AM
How to Change ASP.NET into Classic ASP? GailCG Classic ASP Basics 2 April 22nd, 2009 06:04 PM
Converting Classic Asp to Asp.Net 2.0 vikaspatyal ASP.NET 2.0 Professional 2 October 7th, 2007 06:33 PM
asp.net vs classic asp "URGENT" naeem_ul_hussan Classic ASP Professional 1 August 1st, 2007 03:14 PM
classic asp with asp.net on same iis bostonrose .NET Framework 2.0 6 January 10th, 2007 12:38 PM



All times are GMT -4. The time now is 04:13 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.