序列化 XmlDocument & 通过 HTTPWebRequest 发送

发布于 2024-07-26 18:38:35 字数 1811 浏览 5 评论 0 原文

我试图弄清楚如何正确序列化我的 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();

一些我不确定的事情。 我需要在同一个 HTTPWebRequest 中发送 2 个 XmlDocuments。 我之前尝试过将 XmlDocuments 转换为字符串并连接它们(以发送字符串),但是当我使用 StringBuilder / Writer 时,它会添加:

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

我已经在我的 XmlDocument 对象中声明了,所以现在它在那里两次,我那里不能有 部分。 将 XmlDocuments 转换为字符串然后连接它们并发送是否更容易,或者是否有一种简单的方法按原样发送 XmlDocuments?

编辑:

请参阅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'm trying to figure out how to properly serialize my XmlDocument and send it via a HTTPWebRequest object.

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:

  <?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?

Edit:

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> 

I want my root to be <TrackRequest>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心凉怎暖 2024-08-02 18:38:35

我猜这个问题与上一个问题有关,即如果不先将两个 xml 文档都包装在根节点中,则无法将它们组合成一个文档(C# XmlDocument 节点)。

如果是这种情况,那么您不希望序列化 XmlDocuments 并将它们发送到 WebService。 序列化对象是为了传输/存储实际对象,而不是数据。 您只想向 Web 服务发送数据而不是对象,因此只需连接两个 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);

只需将 sb.ToString() 发送到 WebService。

希望我没有完全搞错了方向。

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

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

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

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

恏ㄋ傷疤忘ㄋ疼 2024-08-02 18:38:35

有效(有点)! 谢谢,代码如下:

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

它仍然没有返回正确的 XML:

<?xml version="1.0" encoding="utf-8" ?> 
  <string xmlns="http://namespace/"><?xml version="1.0" ?> <TrackResponse><Response><...

不知道为什么有 2x

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 version="1.0" encoding="utf-8" ?> 
  <string xmlns="http://namespace/"><?xml version="1.0" ?> <TrackResponse><Response><...

Not sure why there's 2x <?xml version...

忆伤 2024-08-02 18:38:35

我现在使用 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'm doing this now with UPS, instead of building the documents in XML, I just used string builders like so:

... 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文