Solved

Passing XMLdocument to webservice method

Posted on 2009-07-01
5
1,628 Views
Last Modified: 2012-06-27
Hello expert,

I have a webservice created using asp.net/C#. The method in the webservice takes one input parameter as type object.

From my asp.net web application , i am calling the web service and passing a XML document to the webmethod.

It is throwing a error.

Here is my web service code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

namespace PCWebApplication
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PCWebService.Service1 PCWS = new PCWebService.Service1();

            XmlDocument doc = new XmlDocument();
            doc.Load("C:\\Temp\\Test.xml");

            /// Call to web service:
            object result;
           
            result= (object) PCWS.ProcessXMLFile(doc);

            Response.Write((string) result);

        }
    }
}

---------------------------------------------------

Here is my calling code from asp.net web application:

 protected void Page_Load(object sender, EventArgs e)
        {
            PCWebService.Service1 PCWS = new PCWebService.Service1();

            XmlDocument doc = new XmlDocument();
            doc.Load("C:\\Temp\\Test.xml");

            /// Call to web service:
            object result;
           
            result= (object) PCWS.ProcessXMLFile(doc);

            Response.Write((string) result);

        }

------------------------------------------------------------
Error Message: The type System.Xml.XmlDocument may not be used in this context.

any help?

0
Comment
Question by:mani_sai
[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
  • 3
  • 2
5 Comments
 
LVL 8

Author Comment

by:mani_sai
ID: 24757040
Sorry here is my web service code:

using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Xml;
using System.Text;
namespace PCWebService
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public string ProcessXMLFile(object doc)
{
/// process doc as a local variable
///
// Get the root node so we can explore its children
XmlDocument newxml = new XmlDocument();
newxml = (XmlDocument) doc;
string stroutput = newxml.DocumentElement.FirstChild.InnerText;
// Store all the values of the elements in a string
//string allMyChildren = nodRoot.InnerText;
return stroutput;

}
 
}
}
0
 
LVL 21

Accepted Solution

by:
MogalManic earned 500 total points
ID: 24761894
This will NEVER work.  Only classes that are marked [Serializable] can be passed as parameters in a web service.  The XmlDocument class is NOT serializable.

Here is an article on how serialization works:
  http://www.15seconds.com/issue/020903.htm

Web services usually use SoapSerialization which is XML.  Your best bet is to pass the XML as a string and reload the XML on the server.  So your code would be something like this:
client:
            PCWebService.Service1 PCWS = new PCWebService.Service1();

            XmlDocument doc = new XmlDocument();
            doc.Load("C:\\Temp\\Test.xml");

            /// Call to web service:
            object result;
           
            result= (object) PCWS.ProcessXMLFile(doc.DocElement.OuterText);
server:


[WebMethod]
public string ProcessXMLFile(String doc)
{
/// process doc as a local variable
///
// Get the root node so we can explore its children
XmlDocument newxml = new XmlDocument();
newxml.LoadXml(doc);
string stroutput = newxml.DocumentElement.FirstChild.InnerText;
// Store all the values of the elements in a string
//string allMyChildren = nodRoot.InnerText;
return stroutput;
 
} 

Open in new window

0
 
LVL 8

Author Closing Comment

by:mani_sai
ID: 31598897
Thanks
0
 
LVL 8

Author Comment

by:mani_sai
ID: 24790755
When you pass XML as string, is there a size limitation, if yes how much is the limit?
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 24793013
I don't think so.  The only limitation would be the total max length.  Currently the default max length is 4K.  If you want to increase that amount you need to include a HTTPRuntime element in your web config:
http://msdn.microsoft.com/en-us/library/e1f13641%28VS.71%29.aspx
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

Suggested Solutions

Title # Comments Views Activity
Build a string of emails from a gridview 2 30
Sending HTML Formatted Email from ASP.NET 2 55
Shared Service Environment 2 54
C# LINQ query question 11 31
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 video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

734 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