Serialize XmlDocument and send via HTTPWebRequest

I am trying to figure out how to serialize my XmlDocument correctly and send it through an HTTPWebRequest object.

Here is what I still have:

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 don’t know about. I have 2 XmlDocuments that I need to send in the same HTTPWebRequest. I tried converting XmlDocuments to strings and just concatenating them (sending a string), but when I used StringBuilder / Writer, it adds:

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

I already have an ad in my XmlDocument objects, so now it's there twice, and I can't have the <string... part there. Is it easier to convert XmlDocuments to strings, then concatenate them and submit, or is there an easy way to send XmlDocuments as they are?

Edit:

See C # XmlDocument Nodes When I try to convert one of my XmlDocuments to a string, it displays 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>

+1
source share
3 answers

I assume this question is related to the previous of two xml documents that cannot be combined into a single document without wrapping them like in the root node in the first place ( C # XmlDocument Nodes ).

If so, you do not want to serialize XmlDocuments and submit them to WebService. Serialization of objects is intended for transportation / storage of the actual object, not data. You just want to send the webservice data, not the object, just to concatenate the two XML documents and send them.

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

Just send sb.ToString () to the WebService.

I hope I don’t have the wrong end of the stick at all.

+1
source

It works (several)! Thanks heres 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 does not return the correct XML:

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

I don’t know why there 2x <?xml version...

+1
source

I am doing this now with UPS, instead of creating documents in XML, I just used line builders like this:

... in code:

 AddNode("name", "value"); 

... in class:

 private StringBuilder sb; public void AddNode(string name, string value) { sb.Append("<" + name + ">" + value + "</" + name + ">"); } 

I personally find it better because it reduces the load on the server.

-2
source

Source: https://habr.com/ru/post/1381341/


All Articles