我试图弄清楚如何正确序列化我的 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>
发布评论
评论(3)
我猜这个问题与上一个问题有关,即如果不先将两个 xml 文档都包装在根节点中,则无法将它们组合成一个文档(C# XmlDocument 节点)。
如果是这种情况,那么您不希望序列化 XmlDocuments 并将它们发送到 WebService。 序列化对象是为了传输/存储实际对象,而不是数据。 您只想向 Web 服务发送数据而不是对象,因此只需连接两个 xml 文档并发送即可。
只需将 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.
The just send sb.ToString() to the WebService.
Hope I haven't got completely the wrong end of the stick.
有效(有点)! 谢谢,代码如下:
它仍然没有返回正确的 XML:
不知道为什么有 2x
Works (somewhat)! Thanks, heres the code:
It still doesnt return proper XML though:
Not sure why there's 2x
<?xml version...
我现在使用 UPS 执行此操作,而不是在 XML 中构建文档,我只是使用字符串构建器,如下所示:
... in code:
...in class:
我个人认为这样更好,因为它减少了服务器负载。
I'm doing this now with UPS, instead of building the documents in XML, I just used string builders like so:
... in code:
...in class:
I personally think it's better, because it reduces the server load.