Wednesday, March 21, 2012

XmlDocument parameter error

I have the following webservice function definition:

[WebMethod(EnableSession =true)]

[ScriptMethod(ResponseFormat =ResponseFormat.Xml)]

publicXmlDocument requestXML(XmlDocument requestXML)

I am trying to pass the XmlDocument parameter from JavaScript on an Ajax enabled web page:

<scripttype="text/javascript">

function newDocument(text)

{

if (typeof DOMParser !="undefined")

{

return (new DOMParser( )).parseFromString(text,"application/xml");

}

else

{

var doc =new ActiveXObject("MSXML2.DOMDocument");

doc.loadXML(text);

return doc;

}

}

// This function calls the Web Service method.

function request()

{

var entryElem = document.getElementById("entry");var entryText = newDocument("<loginOwner><question>"+entryElem.value+"</question><UserID>visitor</UserID><OwnerName>anonymous</OwnerName></loginOwner>");

improxy.requestXML(entryText, onOK);

}

function onOK(resultXML)

{

alert(resultXML.xml);

}

</script>

I am getting an error: "Microsoft JScript runtime error: Object doesn't support this action". In the debugger, this error occurs in the function: "Sys.Serialization.JavaScriptSerializer._serializeWithBuilder".

Isn't the XmlDocument type supported as a parameter in Ajax?

Thanks,

Eyal

The Client Side document is not compatible with the server side object. You can simply send the xml as plain string.


1. The return value is also a server side XmlDocument and this is translated correctly to the client side object. Can't this be done for a parameter?

2. If I send the XML as string, the webservice complains about wrong data types. Is there another way to tell the server to handle this string as XML?


Hi

I have tested that sending the XML as string to webservice,and it works.

Try this:

Default.aspx

<%@. Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>

<script type="text/javascript">
// function newDocument(text)
// {
// if (typeof DOMParser != "undefined")
// {
// return (new DOMParser( )).parseFromString(text, "application/xml");
// }
// else
// {
// var doc = new ActiveXObject("MSXML2.DOMDocument");
// doc.loadXML(text);
// return doc;
// }
// }

// This function calls the Web Service method.
function request()
{
// var entryElem = document.getElementById("entry");
var entryText = "<loginOwner><question>entryElem</question><UserID>visitor</UserID><OwnerName>anonymous</OwnerName></loginOwner>";
// throw "";
Samples.AspNet.WebService.GetXmlDocument(entryText, onOK);
}

function onOK(result)
{
// Page element to display feedback.
var RsltElem = document.getElementById("ResultId");

var readResult;
if (document.all)
readResult = result.documentElement.firstChild.text;
else
// Firefox
readResult = result.documentElement.firstChild.textContent;
RsltElem.innerHTML = "XmlDocument content: " + readResult;
}
</script>

</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="http://localhost/AJAXEnabledWebSite/WebService.asmx" />
</Services>
</asp:ScriptManager>
<div>
<button id="Button2" onclick="request(); return false;">
Server Time</button>
<span id="ResultId"></span>
</div>
</form>
</body>
</html>

WebService.asmx

<%@. WebService Language="C#" Class="Samples.AspNet.WebService" %>

using System;
using System.Web;
using System.Xml;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;

namespace Samples.AspNet
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class WebService : System.Web.Services.WebService
{
public WebService() { }

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public XmlNode GetXmlDocument(string requestXML)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(requestXML);
return xmlDoc;
}
}
}

Best Regards


More information about this topic:

Passing an XML instance to a Web Service Method :http://objectsharp.com/cs/blogs/matt/archive/2004/10/30/989.aspx

Passing XmlDocument to .Net Webservice from IE client :http://www.dotnet247.com/247reference/msgs/6/32784.aspx


Thank you. I know passing a string works. My web service expects an XmlDocument parameter, not a string.


I tried the links. None of them shows how to pass an XmlDocument with Ajax. Only directly with XMLHTTP.

No comments:

Post a Comment