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()); }
是否理解错误?
因为哪会在这段代码2边加上<string>内容</string>
这个我会,但不是我想要的
另外使用字符串处理肯定是可以的。
也许是你写的方法不对吧。
/// <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就行。
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());
}
<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>
返回字串我会,可返回的2边会加上节点string我不想要这个懂不?
<Output>
<DataArea id="Info" text="返回信息">
<Field id="Code" text="代码">134</Field>
<Field id="ErrMg" text="错误信息">0110</Field>
</DataArea>
</Output>
WebService是会拼上SOAP头的
如果你是要支持HTTP GET,直接写个aspx简单些