为什么返回System.Xml.Linq.XDocument类型时说:
System.Xml.Linq.XDeclaration 无法序列化,因为它没有无参数的构造函数。我该怎么返回已经编辑好的linq.XDocument?
在线急等

解决方案 »

  1.   

    webservice? 
    返回类型 需要能序列化才行 直接返回string xml 格式就行了
      

  2.   


    返回 XDocument.ToString()
      

  3.   

    我把我的格式都已经写好了,通过XDocument.save(a.xml),a.xml就是我想要的内容。但是如果用XDocument.ToString(),返回的结果就会在开头加上:
    <?xml version="1.0" encoding="utf-8" ?> 
      <string xmlns="http://localhost/WebServer/">
    在结果加上:
    </string>
      

  4.   

    你返回的是 String 类型,这是外面套一层是必需的。这是SOAP协议的规则。
    你可以把你的WebMethod改为void。然后直接在Response里写结果。var response = HttpContext.Current.Response;  
    response.ContentType = "application/xml";  
    response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes("xml字符串"));
      

  5.   

    当然看你怎么调用你的WebService了。如果直接用客户端代理调用的话,不必担心外面套的这一层。框架会自己反序列化成string交给你。
    如果你想用HttpWebRequest调用你的WebMethod,就得用我上面说的方法做。
      

  6.   


    请问这段代码该加到什么位置?
    比如说我想返回的结果是通过把System.Data.DataSet的内容通过:
                    var xdoc = new XDocument(new XDeclaration("1.0", "utf-8", null));
                    var root = new XElement("LiveUpdate");
                    foreach (DataRow row in Datads.Tables[0].Rows)
                        root.Add(new XElement("Software",
                                     new XAttribute("app", row["item"]),
                                     new XAttribute("version", row["version"]),
                                     new XAttribute("data", row["time"]),
                                     new XAttribute("address", row["path"])));
                    xdoc.Add(root);
                    xdoc.save("C:\\a.xml");
                   System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
                   xml.Load("C:\\a.xml");
                   return xml;
    写到a.xml中得到的:
    <?xml version="1.0" encoding="utf-8"?>
    <LiveUpdate>
      <Software app="add" version="1002" data="2011-06-14T09:37:44" address="ab" />
    </LiveUpdate>
    这种形式。
    我怎样不使用save和load的方法直接return返回的就是我要的a.xml的格式呢?
      

  7.   

    先确认下,你客户端的调用方式。WebMethod可以直接返回DataSet的。
      

  8.   

    我在webserver端的config里添加的:
    <webServices>
       <protocols>
         <add name="HttpSoap"/>
         <add name="HttpPost"/>
         <add name="HttpGet"/>
         <add name="HttpPostLocalhost"/>
         <add name="Documentation"/>
      </protocols>
    </webServices>客户端通过调用方法传递参数实现的。使用的是HttpPost和HttpGet。
      

  9.   

    我在客户端使用:
    http://localhost/WebServer/Service.asmx/GetUpdateInfo?item=aaa&version=1001
    后得到:
    <string xmlns="http://localhost/WebServer/">
        <LiveUpdate> 
          <Software app="aaa" version="1003" data="2011-06-14T09:37:46" address="ad" />       </LiveUpdate>
    </string>
    但和我想要的:
    <?xml version="1.0" encoding="utf-8"?>
    <LiveUpdate>
      <Software app="add" version="1002" data="2011-06-14T09:37:44" address="ab" />
    </LiveUpdate>
    完全不一样啊。
      

  10.   


    var xdoc = new XDocument(new XDeclaration("1.0", "utf-8", null));
    ...
    xdoc.Add(root);
    var response = HttpContext.Current.Response;
    response.ContentType = "application/xml";
    response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes(xdoc.ToString()));不用return了。WebMethod改为void的。
    http://blog.csdn.net/fangxinggood/archive/2011/03/26/6279233.aspx
      

  11.   

    不好意思啊,我刚接触webserver以前都是些VC++的所以不是特别懂这块。
    我按你给的方法试了一下 得到的结果是:XML 文档只能有一个顶层元素。处理资源 'http://localhost/WebServer/Service.asmx/GetUpdateInfo' 时出错。第 3 行,位置: 15 </LiveUpdate><LiveUpdate>
    --------------^
    这个怎么回事啊?
      

  12.   

    var root = new XElement("LiveUpdate");xdoc里只能有一个root,查查是不是多加了 xdoc.Add(root) ?