Solved

Read XML request in ASP.NET

Posted on 2007-03-19
15
640 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …

739 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