Question

I'm using HttpClient to post xml to a rest service. The problem is the service expects namespace prefix's in fashion that I'm unable to achieve with DataContractSerializer.

Expected xml:

<gto:createRequest xmlns:gto="http://www...com/sign">
    <userId></userId>
    <visibleDataContentType></visibleDataContentType>
    <visibleData></visibleData>
    <hiddenData></hiddenData>
    <expiryInSeconds></expiryInSeconds>
</gto:createRequest>

The object i'm serialzing:

namespace ABC
{
    [DataContract(Name = "createRequest", Namespace = "http://www...com/sign")]
    public class CreateRequest
    {
        [DataMember(Name = "userId")]
        public string UserId { get; set; }

        [DataMember(Name = "visibleDataContentType")]
        public string VisibleDataContentType { get; set; }

        [DataMember(Name = "visibleData")]
        public string VisibleData { get; set; }

        [DataMember(Name = "hiddenData")]
        public string HiddenData { get; set; }

        [DataMember(Name = "expiryInSeconds")]
        public int ExpiryInSeconds { get; set; }
    }
}

I can't get the prefix "gto: createRequest", this what DataContractSerializer outputs:

<createRequest xmlns="http://www...com/sign" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
   <expiryInSeconds></expiryInSeconds>
   <hiddenData></hiddenData>
   <userId></userId>
   <visibleData></visibleData>
   <visibleDataContentType></visibleDataContentType>
</createRequest>

I have tried the old XmlSerializer but with no luck. Any ideas!?
Update: The namespace prefix does not have to be gto: but i has to be there!

Update: the output from Ondrej Svejdars answer that the server doesn't accept:

<gto:createRequest xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:gto="http://www.test.com/sign">
    <gto:expiryInSeconds>60</gto:expiryInSeconds>
    <gto:hiddenData>hidden</gto:hiddenData>
    <gto:userId>123456</gto:userId>
    <gto:visibleData>visible</gto:visibleData>
    <gto:visibleDataContentType>text/plain</gto:visibleDataContentType>
</gto:createRequest>
Was it helpful?

Solution

[Edited to match the gto: only on top element]

You can tweak xml writer:

public class XmlProxyWritter : XmlTextWriter {
  private string m_NS;
  public XmlProxyWritter(string ns, TextWriter w)
    : base(w) {
      m_NS = ns;
  }
  public XmlProxyWritter(string ns, Stream w, Encoding encoding)
    : base(w, encoding) {
      m_NS = ns;
  }
  public XmlProxyWritter(string ns, string filename, Encoding encoding)
    : base(filename, encoding) {
      m_NS = ns;
  }

  public override string LookupPrefix(string ns) {
    if (string.Compare(ns, m_NS, StringComparison.OrdinalIgnoreCase) == 0) {
      return "gto";
    }
    return base.LookupPrefix(ns);
  }

  public override void WriteStartElement(string prefix, string localName, string ns) {
    if (string.IsNullOrEmpty(prefix) && !string.IsNullOrEmpty(ns)) {
      prefix = LookupPrefix(ns);
    }
    base.WriteStartElement(prefix, localName, ns);
  }
}

Business class:

[XmlRoot(ElementName = "createRequest", Namespace = "http://www.test.com/sign")]
public class CreateRequest {
  [XmlElement(ElementName="userId", Namespace = "")]
  public string UserId { get; set; }

  [XmlElement(ElementName = "visibleDataContentType", Namespace = "")]
  public string VisibleDataContentType { get; set; }

  [XmlElement(ElementName = "visibleData", Namespace = "")]
  public string VisibleData { get; set; }

  [XmlElement(ElementName = "hiddenData", Namespace = "")]
  public string HiddenData { get; set; }

  [XmlElement(ElementName = "expiryInSeconds", Namespace = "")]
  public int ExpiryInSeconds { get; set; }
}    

Call example (where http://www.test.com/sign is the namespace of CreateRequest)

  string result;
  var serXml = new XmlSerializer(typeof(CreateRequest));

  using (var stream = new MemoryStream()) {
    using (var writer = new XmlProxyWritter("http://www.test.com/sign", stream, Encoding.UTF8)) {
      serXml.Serialize(writer, new CreateRequest {
        ExpiryInSeconds = 1,
        HiddenData = "my preasures",
        UserId = "Pepa"
      });
    }
    result = Encoding.UTF8.GetString(stream.ToArray());
  }

Output:

<?xml version="1.0" encoding="utf-8"?>
<gto:createRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:gto="http://www.test.com/sign">
  <userId>Pepa</userId>
  <hiddenData>my preasures</hiddenData>
  <expiryInSeconds>1</expiryInSeconds>
</gto:createRequest>

Which works for you (I hope) but it feels like kind of hacking; maybe the proper solution here would be to "teach" server the correct xml format ? :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top