web返回类型 为什么返回System.Xml.Linq.XDocument类型时说:System.Xml.Linq.XDeclaration 无法序列化,因为它没有无参数的构造函数。我该怎么返回已经编辑好的linq.XDocument?在线急等 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 webservice? 返回类型 需要能序列化才行 直接返回string xml 格式就行了 返回 XDocument.ToString() 我把我的格式都已经写好了,通过XDocument.save(a.xml),a.xml就是我想要的内容。但是如果用XDocument.ToString(),返回的结果就会在开头加上:<?xml version="1.0" encoding="utf-8" ?> <string xmlns="http://localhost/WebServer/">在结果加上:</string> 你返回的是 String 类型,这是外面套一层是必需的。这是SOAP协议的规则。你可以把你的WebMethod改为void。然后直接在Response里写结果。var response = HttpContext.Current.Response; response.ContentType = "application/xml"; response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes("xml字符串")); 当然看你怎么调用你的WebService了。如果直接用客户端代理调用的话,不必担心外面套的这一层。框架会自己反序列化成string交给你。如果你想用HttpWebRequest调用你的WebMethod,就得用我上面说的方法做。 请问这段代码该加到什么位置?比如说我想返回的结果是通过把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的格式呢? 先确认下,你客户端的调用方式。WebMethod可以直接返回DataSet的。 我在webserver端的config里添加的:<webServices> <protocols> <add name="HttpSoap"/> <add name="HttpPost"/> <add name="HttpGet"/> <add name="HttpPostLocalhost"/> <add name="Documentation"/> </protocols></webServices>客户端通过调用方法传递参数实现的。使用的是HttpPost和HttpGet。 我在客户端使用: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>完全不一样啊。 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 不好意思啊,我刚接触webserver以前都是些VC++的所以不是特别懂这块。我按你给的方法试了一下 得到的结果是:XML 文档只能有一个顶层元素。处理资源 'http://localhost/WebServer/Service.asmx/GetUpdateInfo' 时出错。第 3 行,位置: 15 </LiveUpdate><LiveUpdate>--------------^这个怎么回事啊? var root = new XElement("LiveUpdate");xdoc里只能有一个root,查查是不是多加了 xdoc.Add(root) ? 看看水有多深 大家看下面的界面做的方法是否合理,欢迎提出意见 TCP问题,求高手解 统计销售额 想做个多个线程的,但不知怎么下手 数码相机编程 又问简单问题白拿分啦。关于session 的timeout MSDN的下载 C# 新手,写的几个数据库操作方法,有没有需要改进的地方,请大侠指教 一个socket 的问题,BeginConnect这个方法是不是不受blocking的影响啊~!! 如何实现软件和内嵌网页消息互通啊 关于正则的小问题急求
返回类型 需要能序列化才行 直接返回string xml 格式就行了
返回 XDocument.ToString()
<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://localhost/WebServer/">
在结果加上:
</string>
你可以把你的WebMethod改为void。然后直接在Response里写结果。var response = HttpContext.Current.Response;
response.ContentType = "application/xml";
response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes("xml字符串"));
如果你想用HttpWebRequest调用你的WebMethod,就得用我上面说的方法做。
请问这段代码该加到什么位置?
比如说我想返回的结果是通过把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的格式呢?
<webServices>
<protocols>
<add name="HttpSoap"/>
<add name="HttpPost"/>
<add name="HttpGet"/>
<add name="HttpPostLocalhost"/>
<add name="Documentation"/>
</protocols>
</webServices>客户端通过调用方法传递参数实现的。使用的是HttpPost和HttpGet。
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>
完全不一样啊。
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
我按你给的方法试了一下 得到的结果是:XML 文档只能有一个顶层元素。处理资源 'http://localhost/WebServer/Service.asmx/GetUpdateInfo' 时出错。第 3 行,位置: 15 </LiveUpdate><LiveUpdate>
--------------^
这个怎么回事啊?