序列化的XmlDocument和放大器;通过发送HttpWebRequest的 [英] Serialize XmlDocument & send via HTTPWebRequest

查看:135
本文介绍了序列化的XmlDocument和放大器;通过发送HttpWebRequest的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何正确序列化我的XmlDocument并通过HttpWebRequest对象发送出去。

I'm trying to figure out how to properly serialize my XmlDocument and send it via a HTTPWebRequest object.

这是我迄今:

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();

这几件事情,我不确定。我有2 XmlDocuments我需要发送相同的HttpWebRequest。我试过之前给XMLDocuments转换为字符串,只是将它们连接起来(发送字符串),但是当我用的StringBuilder /写它增加了:

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:

  <?xml version="1.0" encoding="utf-8" ?> 
  <string xmlns="http://myNameSpace/">

我已经有声明,我XmlDocument的对象,所以现在它在那里两次,我不能有&LT;串... 部分在那里。是不是更容易给XMLDocuments转换为字符串,然后将它们串联并发送还是有一个简单的方法来发送给XMLDocuments因为他们是谁?

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?

编辑:

请参阅 C#的XmlDocument节点 当我尝试将我的XmlDocuments一个字符串,显示为

See C# XmlDocument Nodes When I try to convert one of my XmlDocuments to a string it shows up as

  <?xml version="1.0" encoding="utf-8" ?> 
  <string xmlns="http://myNamespace/">
      <TrackRequest>
         <Request>
            <TransactionReference>
                <CustomerContext>whatever</CustomerContext>
            </TransactionReference>
         </Request>
         <TrackingNumber>123</TrackingNumber>
      </TrackRequest>
   </string> 

我想我的根是&LT; TrackRequest&GT;

推荐答案

我猜这个问题没有在包装他们两人是关系到previous一个约2 XML文档不能够被合并成一个文件根节点第一(<一href="http://stackoverflow.com/questions/1104549/c-xmldocument-nodes/1104586#1104586">http://stackoverflow.com/questions/1104549/c-xmldocument-nodes/1104586#1104586).

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 (http://stackoverflow.com/questions/1104549/c-xmldocument-nodes/1104586#1104586).

如果是这样的话,那么你不想被连载给XMLDocuments并将它们发送到web服务。序列化的对象是用于传输/存储实际对象,而不是数据。你想只是把Web服务中的数据不是对象,因此只将两者连接起来XML文件并发送。

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.

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);

刚刚发送sb.ToString(),以WebService的。

The just send sb.ToString() to the WebService.

希望我没有这方面的棍子完全错误的结束。

Hope I haven't got completely the wrong end of the stick.

这篇关于序列化的XmlDocument和放大器;通过发送HttpWebRequest的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆