This is a continuation of my previous question regarding the Expect100Continue Header not being able to be removed in Windows Store apps, for reference: ServicePoint.Expect100Continue for Windows Store Apps
I stepped back now and am now using the HttpClient to manually POST HttpContent to the Webservice. I am using classes like this:
[XmlRoot(ElementName = "myRootElement", Namespace = "myNamespace.com")]
public class MyRootElement
{
[XmlElement(Namespace = "myNamespace.com", ElementName = "myParameter")]
public string MyParameter { get; set; }
}
To serialize and construct the body of the HttpContent, i am using serialization code like this:
using (MemoryStream stream = new MemoryStream())
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Encoding = Encoding.UTF8;
settings.Indent = false;
settings.NamespaceHandling = NamespaceHandling.Default;
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "myNamespace.com");
using (XmlWriter xmlWriter = XmlWriter.Create(stream, settings))
{
XmlSerializer ser = new XmlSerializer(input.GetType());
ser.Serialize(xmlWriter, input, ns);
}
var arr = stream.ToArray();
body = Encoding.UTF8.GetString(arr, 0, arr.Length);
}
var result = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
body
+ "</s:Body></s:Envelope>";
However the output is flawed. The namespace is only written at the root element but not on the parameter element. There is a “?” character at the beginning of body i am unable to remove or find the source of.
Also i’d like to know if there is a better way then string concatonation to enclose the body in a SOAP envelope.
Solved the “?” issue by replacing the MemoryStream with a StringBuilder. The namespace issues where resolved by using two different namespaces for the XmlRoot and the XmlElements. It feels clunky and unnecessary complicated but “it works”.