如何从Web服务返回XDocument? [英] How do I return an XDocument from a web service?

查看:99
本文介绍了如何从Web服务返回XDocument?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我正在尝试编写我的第一个Web服务。我需要它从请求者使用xml文档并将xml文档返回给它们。这是我的代码:



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;
        }

    }
}





但是当我在VS 2010中运行它时我收到此错误:





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?

推荐答案

这些链接应该提供足够的信息来解决你的问题:



如何从Xdocument中将Xml作为字符串 [ ^ ]



填充XDocument来自字符串 [ ^ ]



亲切问候,
These links should provide enough information to solve your problem:

How to get Xml as string from Xdocument[^]

Populate XDocument from String[^]

Kind regards,


这篇关于如何从Web服务返回XDocument?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆