DataContractSerializer - 命名空间问题

时间:2013-11-29 12:47:04

标签: c# xml namespaces datacontractserializer

我正在使用HttpClient将xml发布到休息服务。问题是服务期望使用DataContractSerializer无法实现的名称空间前缀。

预期xml:

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

我正在序列化的对象:

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

我无法获得前缀“ gto: createRequest”,这就是DataContractSerializer输出的内容:

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

我尝试过旧的XmlSerializer,但没有运气。任何想法!? 更新:命名空间前缀不必是 gto:,但我必须在那里!

更新:Ondrej Svejdars的输出回答服务器不接受:

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

1 个答案:

答案 0 :(得分:0)

[编辑以匹配gto:仅限于顶部元素]

你可以调整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);
  }
}

商务舱:

[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; }
}    

调用示例(其中http://www.test.com/sign是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());
  }

输出:

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

哪个适合你(我希望),但感觉就像是一种黑客行为;也许这里适当的解决方案是“教”服务器正确的xml格式? :)