当我使用“WebRequest”时,我遇到了xml结果的问题。但如果我在项目中使用Web引用,我可以获得正确的xml格式。我不能在我的项目中使用web引用一些限制。什么是错的,我该怎么办呢。这是代码和结果。
WebRequest request = WebRequest.Create("http://xxx:xx/service.asmx ");
request.Method = "POST";
XmlDocument _xmlDoc = new XmlDocument();
_xmlDoc.Load(_inputPath);
string postData = _xmlDoc.OuterXml;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "text/xml; charset=utf-8";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
byte[] buffer = Encoding.UTF8.GetBytes(responseFromServer);
System.Data.DataSet ds = new System.Data.DataSet();
using (System.IO.MemoryStream stream = new System.IO.MemoryStream(buffer))
{
System.Xml.XmlReader _reader = System.Xml.XmlReader.Create(stream);
ds.ReadXml(_reader);
ds.WriteXml(_result);
ds.Dispose();
}
**result for above code**
<?xml version="1.0" standalone="yes"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<QueryDataSetXMLStringResponse xmlns="sctt.Service">
<QueryDataSetXMLStringResult><NewDataSet>
<Table>
<GRN_No>GRN-15040089</GRN_No>
</NewDataSet></QueryDataSetXMLStringResult>
</QueryDataSetXMLStringResponse>
</soap:Body>
</soap:Envelope>
**Expected result and get this if i use web refrences**
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Table>
<GRN_No>GRN-15040089</GRN_No>
</Table>
</NewDataSet>
Request Env and body
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<QueryDataSetXMLString xmlns="sctt.Service">
<Date>07Jul2015</Date>
</QueryDataSetXMLString>
</soap:Body>
</soap:Envelope>
答案 0 :(得分:0)
将Web方法的返回类型设置为XmlDocument
{{1}}