Solved

Read XML request in ASP.NET

Posted on 2007-03-19
15
631 Views
Last Modified: 2008-02-01
I'm trying to port an ajax app from ASP to ASP.NET (not sure if its 2.0, or how to tell). I have an [index.asp] page that makes ajax calls to [process.asp] page.   I use this code to grab the background xml requests arriving at [process.asp]  :
------------------------------------------------------------
Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load(request)
------------------------------------------------------------

How do i do the same thing for ASP.NET ? ...  here's an example xml string that's sent :

-------------------------------------
<?xml version="1.0"?>
<user task="userLookup">
<queryString>SELECT firstName FROM users</queryString>
</user>
-------------------------------------

Please help !
0
Comment
Question by:Gitcho
  • 6
  • 6
15 Comments
 
LVL 23

Expert Comment

by:Christopher Kile
ID: 18750376
You can use virtually the same code if you create an Interop assembly that wraps the COM object you're using.  I don't necessarily recommend that, but without knowing all the functionality you might be using, I don't really know where to direct you in the .NET CLR functions for XML management.
0
 
LVL 5

Author Comment

by:Gitcho
ID: 18750895
thx, but I'd prefer the syntax for reading an XML request in ASP.net ...  
0
 
LVL 23

Expert Comment

by:Christopher Kile
ID: 18751244
Well, just to be clear, it is not a case of "what is the syntax" but "what libraries are we using, what functions are you calling, and what parameters do they expect".  

With that in mind, I have never worked with the XMLDOM control before and do not know exactly what parameters IT expects.  For instance, in the statement

xmlDoc.load(request)

what type of argument is request?

Regardless of my own questions, I believe the information in the following two links will be very helpful to you:

http://msdn2.microsoft.com/en-us/library/hf9hbf87(VS.80).aspx
http://msdn2.microsoft.com/en-us/library/system.xml.xmldocument(VS.80).aspx


0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 5

Author Comment

by:Gitcho
ID: 18761023
sorry cpkilekofp ... that doesn't help ... i'm familiar with those links (XMLDOM overview).  What I need is the syntax for reading XML data posted to an ASP.NET page.  I provided an example of what type the "request" object is in my original question (the XML string), but "xmlDoc.load(request)" is classic ASP syntax, and not what I'm looking for.
0
 
LVL 23

Expert Comment

by:Christopher Kile
ID: 18764578
OK...you have a response coming from a URL from which you have requested XML information, and you want to load it as XML...I think I understand you now, the "request" thing threw me.

When you say "AJAX calls", what object is mediating the request?  I have no idea what this looks like in ASP, but in ASP.NET my assumption is that you're using some descendant of WebRequest.  You'll tell the WebRequest to GetResponse, which will return a WebResponse.  The body of the response is returned as a Stream by GetResponseStream.  It is this body that will contain your XML. SO....

Dim xmlDoc as New System.XML
Dim urlString as String = <whatever URL the XML is being provided by>

xmlDoc.Load(System.WebRequest.Create(urlString).GetResponse().GetResponseStream())

...should be the code to get that XML and load it into an XML document.
0
 
LVL 5

Author Comment

by:Gitcho
ID: 18801502
i get this error :

Compiler Error Message: BC30182: Type expected.
0
 
LVL 5

Author Comment

by:Gitcho
ID: 18801508
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30182: Type expected.

Source Error:


Line 1:  <%
Line 2:  Dim xmlDoc as New System.XML
Line 3:  Dim urlString as String = "<whatever URL the XML is being provided by>"
Line 4:  
 
0
 
LVL 5

Author Comment

by:Gitcho
ID: 18801510
error is on line 2
0
 
LVL 23

Expert Comment

by:Christopher Kile
ID: 18802277
Ouch, sorry.

Dim xmlDoc as New System.XML

should be

Dim xmlDoc as New System.Xml.XmlDocument
0
 
LVL 5

Author Comment

by:Gitcho
ID: 18803122
I think you're a bit confused ... the XML is arriving at the page as POST, not being retrieved by the page ... the receiving aspx page is simply a processor that returns more xml ...


This is the code I use on the receiving ASP page :

Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load(request)

This is the javascript code that sends :

function sendXML(sData,querystring,resultHandlerFunction,targetPage)
{
      targetPage = targetPage ? targetPage : constXMLpage;
      url = targetPage + "?rand=" + (new Date().getTime()) + "&" + querystring;
      xmlhttp.Open("POST", url, true, "", "");
      xmlhttp.setRequestHeader("Content-Type","text/xml");
      xmlhttp.Send (sData);
      window.status = "Load in progress ........ ";
      xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4) {
                  if (resultHandlerFunction) {
                        eval(resultHandlerFunction);
                  } else {
                        XMLResponseHandler(xmlhttp.ResponseText,xmlhttp.ResponseXML);
                  }
                  window.status = "XML object loaded successfully.";
            }
      };
};

0
 
LVL 23

Accepted Solution

by:
Christopher Kile earned 250 total points
ID: 18803782
Mmm, yes, I did get confused.  

I'm now assuming that you've already parceled out request As String from the content sent to you, so request contains simply the XML you sent.

Dim xmlDoc as New System.Xml.XmlDocument
xmlDoc.Load(New StringReader(request))

This will load the xmlDoc for you. There is no Load function for an XML string itself, the only Load that accepts a string argument expects a URL in the string, not the XML itself, but there is a Load function for a TextReader, and a StringReader is a class derived from TextReader, so it is compatible.
0
 
LVL 23

Expert Comment

by:Christopher Kile
ID: 19056919
I'd bet quite a bit of, hmmm, vanity that my last post described his solution, which by this time seemed to me to be a method for loading an XMLDocument from a String, and that all that remained would be to debug and test.  
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now