Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am trying to write my first web service. I need it to consume an xml document from the requester and return an xml document to them. Here is my code:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml.Linq;

namespace Tester.svcs
{

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    public class subcheck : System.Web.Services.WebService
    {

        public subcheck()
        {

        }

        [WebMethod]
        public XDocument CheckSubsciption()
        {
            string userXML = "<?xml version='1.0' encoding='UTF-8'?><Customer><ID>2342</ID></Customer>";
            string Key = "";
           
            XDocument recievedXMLdoc = XDocument.Parse(userXML);

            licKey = QueryTheData(recievedXMLdoc);

            XDocument returningXMLdoc = ReturnData(Key);
			
            return returningXMLdoc;
        }

        private XDocument ReturnData(string key)
        {


            XDocument doc = new XDocument(
                new XDeclaration("1.0", "utf-8", "yes"),
                new XComment("Customer Status"),
                new XElement("Status",
                    new XElement("Key", key),
                    new XElement("Message", "New fonts added January 21st.")));

            return doc;
        }

        private string QueryTheData(XDocument doc)
        {
            string returnData = "";

            var data = doc.Descendants("Status")
                .Select(i => i.Element("Key").Value)
                .Distinct();

            foreach (var p in data)
                returnData = p.ToString();

            return returnData;
        }

    }
}


But when I run it in VS 2010 I get this error:

HTML
Server Error in '/' Application.

System.Xml.Linq.XDeclaration cannot be serialized because it does not have a parameterless constructor.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: System.Xml.Linq.XDeclaration cannot be serialized because it does not have a parameterless constructor.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[InvalidOperationException: System.Xml.Linq.XDeclaration cannot be serialized because it does not have a parameterless constructor.]

[InvalidOperationException: Cannot serialize member 'System.Xml.Linq.XDocument.Declaration' of type 'System.Xml.Linq.XDeclaration', see inner exception for more details.]
   System.Xml.Serialization.StructModel.CheckSupportedMember(TypeDesc typeDesc, MemberInfo member, Type type) +889709


I have searched all over and I cannot figure out what I am doing wrong. Can someone help me please?
Posted
Updated 28-Jan-14 16:27pm
v2
Comments
Programm3r 29-Jan-14 1:51am    
"...I have searched all over and I cannot figure out what I am doing wrong..." - The exception clearly states the issue: System.Xml.Linq.XDeclaration cannot be serialized because it does not have a parameterless constructor

Instead of asking "How do I return an XDocument from a web service", rather ask yourself, "Why do I want to return an XDocument from a web service"? Surely there are better ways of doing this... XML, JSON...

1 solution

These links should provide enough information to solve your problem:

How to get Xml as string from Xdocument[^]

Populate XDocument from String[^]

Kind regards,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900