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:
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:
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?