我想弄清楚如何正確序列化我的XmlDocument並通過HTTPWebRequest對象發送它。序列化XmlDocument&通過HTTPWebRequest發送
這裏是我迄今:
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();
有幾件事情,我不確定。我有兩個XmlDocuments我需要在相同的HTTPWebRequest中發送。我已經嘗試過給XMLDocuments轉換爲字符串,只是將它們連接起來(發送字符串),但是當我用的StringBuilder /寫它補充說:
<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://myNameSpace/">
我已經在我的XmlDocument聲明所以現在反對它在那裏兩次,我不能有<string...
部分在那裏。將XmlDocuments轉換爲字符串然後將它們連接起來併發送它或者是否有一種簡單的方式來發送XmlDocuments?
編輯:
見C# XmlDocument Nodes 當我嘗試我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>
我想我的根是<TrackRequest>
你可以顯示你想要的XML看起來像什麼嗎?您無法連續發送兩個文檔 - 需要有一個根元素。 – 2009-07-09 16:57:14