C# 序列化 XmlDocument 并通过 HTTPWebRequest 发送
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1105146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Serialize XmlDocument & send via HTTPWebRequest
提问by Chris Klepeis
I'm trying to figure out how to properly serialize my XmlDocument and send it via a HTTPWebRequest object.
我试图弄清楚如何正确序列化我的 XmlDocument 并通过 HTTPWebRequest 对象发送它。
Here's what I have thus far:
这是我到目前为止所拥有的:
Stream requestStream;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://wwwcie.ups.com/ups.app/xml/Track");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
requestStream = request.GetRequestStream();
XmlSerializerNamespaces xsm = new XmlSerializerNamespaces();
xsm.Add("", ""); // remove namespace
XmlSerializer ser = new XmlSerializer(xmlRequest.GetType());
ser.Serialize(requestStream, xmlRequest);
requestStream.Write(postData, 0, postData.Length);
requestStream.Close();
A few things I'm uncertain about. I have 2 XmlDocuments I need to send in the same HTTPWebRequest. I've tried before to convert the XmlDocuments to strings and just concatenate them (to send the string) but when I used the StringBuilder / Writer it adds:
有几件事我不确定。我有 2 个 XmlDocuments 我需要在同一个 HTTPWebRequest 中发送。我之前尝试过将 XmlDocuments 转换为字符串,然后将它们连接起来(发送字符串),但是当我使用 StringBuilder / Writer 时,它会添加:
<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://myNameSpace/">
I already have the declaration in my XmlDocument objects so now its in there twice, and I cannot have the <string...
part in there. Is it easier to convert the XmlDocuments to strings then concatenate them and send that or is there a easy way to send the XmlDocuments as they are?
我已经在我的 XmlDocument 对象中有声明,所以现在它在那里两次,我不能<string...
在那里有一部分。是否更容易将 XmlDocuments 转换为字符串,然后将它们连接起来并发送,或者是否有一种简单的方法可以按原样发送 XmlDocuments?
Edit:
编辑:
See C# XmlDocument NodesWhen I try to convert one of my XmlDocuments to a string it shows up as
请参阅C# XmlDocument 节点当我尝试将我的 XmlDocuments 之一转换为字符串时,它显示为
<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://myNamespace/">
<TrackRequest>
<Request>
<TransactionReference>
<CustomerContext>whatever</CustomerContext>
</TransactionReference>
</Request>
<TrackingNumber>123</TrackingNumber>
</TrackRequest>
</string>
I want my root to be <TrackRequest>
我希望我的根是 <TrackRequest>
采纳答案by Russell Troywest
I'm guessing this question is related to the previous one about 2 xml documents not being able to be combined into one document without wrapping them both in a root node first (C# XmlDocument Nodes).
我猜这个问题与上一个有关 2 个 xml 文档无法组合成一个文档而不首先将它们都包装在根节点(C# XmlDocument 节点)中有关。
If that is the case then you don't want to be serialising the XmlDocuments and sending them to the WebService. Serializing the objects is for transporting/storing the actual object, not the data. You want to just send the webservice the data not the object so just concatenate the two xml documents and send that.
如果是这种情况,那么您不希望序列化 XmlDocuments 并将它们发送到 WebService。序列化对象是为了传输/存储实际对象,而不是数据。您只想向网络服务发送数据而不是对象,因此只需连接两个 xml 文档并发送即可。
use XmlDocument.OuterXml to get the XmlDocument as a string. ie:
XmlDocument doc1 = new XmlDocument();
doc.LoadXml("Some XML");
XmlDocument doc2 = new XmlDocument();
doc2.LoadXml("Some other XML");
StringBuilder sb = new StringBuilder();
sb.Append(doc1.OuterXml);
sb.Append(doc2.OuterXml);
The just send sb.ToString() to the WebService.
只是将 sb.ToString() 发送到 WebService。
Hope I haven't got completely the wrong end of the stick.
希望我没有完全弄错棍子的一端。
回答by Chris Klepeis
Works (somewhat)! Thanks, heres the code:
有效(有点)!谢谢,代码如下:
Stream requestStream;
Stream responseStream;
WebResponse response;
StreamReader sr;
byte[] postData;
string postString;
postString = xmlAccess.OuterXml + xmlRequest.OuterXml;
postData = Encoding.UTF8.GetBytes(postString);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://wwwcie.ups.com/ups.app/xml/Track");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
requestStream = request.GetRequestStream();
requestStream.Write(postData, 0, postData.Length);
requestStream.Close();
response = request.GetResponse();
responseStream = response.GetResponseStream();
sr = new StreamReader(responseStream);
return sr.ReadToEnd();
It still doesnt return proper XML though:
尽管如此,它仍然没有返回正确的 XML:
<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://namespace/"><?xml version="1.0" ?> <TrackResponse><Response><...
Not sure why there's 2x <?xml version...
不知道为什么有 2x <?xml version...
回答by Chris Klepeis
I'm doing this now with UPS, instead of building the documents in XML, I just used string builders like so:
我现在用 UPS 做这个,而不是用 XML 构建文档,我只是使用字符串构建器,如下所示:
... in code:
...在代码中:
AddNode("name", "value");
...in class:
...在班上:
private StringBuilder sb;
public void AddNode(string name, string value)
{
sb.Append("<" + name + ">" + value + "</" + name + ">");
}
I personally think it's better, because it reduces the server load.
我个人认为它更好,因为它减少了服务器负载。