webserivce如何返回这样格式

飞天凤凰601 2011-08-12 04:38:14

<?xml version=“1.0” encoding="GB2312" ?>
<Output>
<DataArea id="Info" text="返回信息">
<Field id="Code" text="代码"> 134 </Field>
<Field id="ErrMsg" text="错误信息"> 0110 </Field>
</DataArea>
</Output>




谢谢大家
...全文
145 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
leiziaitudou 2011-08-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 liuhongzhe 的回复:]
自己在server里拼个xml字符串,然后返回字符串呗。
是否理解错误?
[/Quote]
这样就可以了!
飞天凤凰601 2011-08-16
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 active 的回复:]

你调用得WebService是用HTTP Get方式调用吧?
WebService是会拼上SOAP头的
如果你是要支持HTTP GET,直接写个aspx简单些
[/Quote]
对httpget

active 2011-08-16
  • 打赏
  • 举报
回复
你调用得WebService是用HTTP Get方式调用吧?
WebService是会拼上SOAP头的
如果你是要支持HTTP GET,直接写个aspx简单些
huangwenquan123 2011-08-16
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 oysy 的回复:]
引用 11 楼 diandian82 的回复:
写个ToXML方法算了,这么麻烦呢


返回字串我会,可返回的2边会加上节点string

我不想要这个。。。懂不?
[/Quote]刚又测试了下,返回的是这个,没有你说的那个string,测试代码就是7楼提供的。

<?xml version="1.0"?>
<Output>
<DataArea id="Info" text="返回信息">
<Field id="Code" text="代码">134</Field>
<Field id="ErrMg" text="错误信息">0110</Field>
</DataArea>
</Output>
飞天凤凰601 2011-08-16
  • 打赏
  • 举报
回复
顶上去,继续求答案
飞天凤凰601 2011-08-15
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 diandian82 的回复:]
写个ToXML方法算了,这么麻烦呢
[/Quote]

返回字串我会,可返回的2边会加上节点string

我不想要这个。。。懂不?
diandian82 2011-08-15
  • 打赏
  • 举报
回复
写个ToXML方法算了,这么麻烦呢
飞天凤凰601 2011-08-15
  • 打赏
  • 举报
回复
7楼的还是不对,返回来的,是这样

<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://57.1.57.1/"><?xml version="1.0"?> <Output> <DataArea id="Info" text="返回信息"> <Field id="Code" text="代码">134</Field> <Field id="ErrMg" text="错误信息">0110</Field> </DataArea> </Output></string>
飞天凤凰601 2011-08-12
  • 打赏
  • 举报
回复
测试后,我再结贴吧
飞天凤凰601 2011-08-12
  • 打赏
  • 举报
回复
先谢谢7楼及其他兄弟,感觉这个应该可以了
但我需要周一上单位测试才行,现在家里测试不了
huangwenquan123 2011-08-12
  • 打赏
  • 举报
回复

public class Output
{
public Output(){ }
[XmlElement(ElementName = "DataArea")]
public DataArea dataArea;
}
public class DataArea
{
public DataArea()
{

}
[XmlAttribute(AttributeName = "id")]
public string id;
[XmlAttribute(AttributeName = "text")]
public string text;
[XmlElement(ElementName = "Field")]
public Field[] field;
}
public class Field
{
public Field() { }
public Field(string id, string text, string html)
{
this.id = id;
this.text = text;
this.html = html;
}
[XmlAttribute(AttributeName="id")]
public string id;
[XmlAttribute(AttributeName = "text")]
public string text;
[XmlText]
public string html;
}
//序列化后该类后返回
[WebMethod]
public string StreamXml() {
DataArea da = new DataArea();
da.id = "Info";
da.text = "返回信息";
da.field = new Field[] { new Field("Code", "代码", "134"), new Field("ErrMg", "错误信息", "0110") };
Output op = new Output();
op.dataArea = da;
MemoryStream ms = new MemoryStream();
XmlSerializer xml = new XmlSerializer(typeof(Output));
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
xml.Serialize(ms, op, ns);
return Encoding.UTF8.GetString(ms.ToArray());
}
mjp1234airen4385 2011-08-12
  • 打赏
  • 举报
回复
一种是使用xmlDocument处理,
另外使用字符串处理肯定是可以的。
也许是你写的方法不对吧。
/// <summary>
/// 将传入的参数以xml格式返回
/// </summary>
/// <param name="StrValue"></param>
/// <returns></returns>
public static XmlDataDocument ReturnResultXml(string StrValue)
{
XmlDataDocument xmlDataResult = new XmlDataDocument();
System.IO.Stream DataStream = new System.IO.MemoryStream();
System.Xml.XmlTextWriter xmltextwriterData = new XmlTextWriter(DataStream, System.Text.Encoding.UTF8);
xmltextwriterData.WriteStartDocument();
xmltextwriterData.WriteStartElement("NewDataset");
xmltextwriterData.WriteStartElement("ReturnResult");
xmltextwriterData.WriteElementString("ReturnValue", StrValue);
xmltextwriterData.WriteEndElement();
xmltextwriterData.WriteEndElement();
xmltextwriterData.Flush();
DataStream.Position = 0;
xmlDataResult.Load(DataStream);
xmltextwriterData.Close();
DataStream.Close();
DataStream.Dispose();

return xmlDataResult;
}
}

你可以以这为基础改动,UTF8换成gb2312就行。
  • 打赏
  • 举报
回复
你可以返回内容,然后生成一个那样格式的XML文件
鸭梨山大帝 2011-08-12
  • 打赏
  • 举报
回复
没看懂.

是不是要定义一个类,让其序列化XML时生成这种格式的XML?
飞天凤凰601 2011-08-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 liuhongzhe 的回复:]
自己在server里拼个xml字符串,然后返回字符串呗。
是否理解错误?
[/Quote]
返回字串肯定不行的,
因为哪会在这段代码2边加上<string>内容</string>
这个我会,但不是我想要的
劉宏哲 2011-08-12
  • 打赏
  • 举报
回复
我知道了,你需要把“<”和“>”进行编码,不然部允许传。
劉宏哲 2011-08-12
  • 打赏
  • 举报
回复
自己在server里拼个xml字符串,然后返回字符串呗。
是否理解错误?

110,525

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧