我如何XmlSerializer的名称空间添加到属性中嵌套的对象? [英] How do I get the XMLSerializer to add namespaces to attributes in nested objects?

查看:127
本文介绍了我如何XmlSerializer的名称空间添加到属性中嵌套的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我所得到的:

 <例如:测试SOAP:mustUnderstand属性=1的xmlns:EX =htt​​p://www.example.com/namespace>
  <例如:A型=LOREM>存有< /例如:A>
< / EX:试验>
 

这就是我想要的(请注意,该类型属性是pfixed与前$ P $)

 <例如:测试SOAP:mustUnderstand属性=1的xmlns:EX =htt​​p://www.example.com/namespace>
  <例如:一个EX:TYPE =LOREM>存有< /例如:A>
< / EX:试验>
 

这是我的code:

  [XmlType将(命名空间=htt​​p://www.example.com/namespace)
  [XmlRoot(恩,命名空间=htt​​p://www.example.com/namespace)
  公共类TestSoapHeader:SOAPHEADER {
    私人TestSoapHeaderTypeValuePair _a;

    公共TestHeader(){
      MustUnderstand的=真实;
    }

    [XmlNamespaceDeclarations]
    公共XmlSerializerNamespaces xmlsn {
      得到 {
        XmlSerializerNamespaces XSN =新XmlSerializerNamespaces();
        xsn.Add(恩,http://www.example.com/namespace);
        返回XSN;
      }
      组 { }
    }

    公共TestSoapHeaderTypeValuePair A {
      {返回_a; }
      集合{_a =价值; }
    }

  }

  [XmlType将(命名空间=htt​​p://www.example.com/namespace)
  公共类TestSoapHeaderTypeValuePair {
    私人字符串_type;
    私人字符串_value;

    [XmlNamespaceDeclarations]
    公共XmlSerializerNamespaces xmlsn
    {
      得到
      {
        XmlSerializerNamespaces XSN =新XmlSerializerNamespaces();
        xsn.Add(恩,http://www.example.com/namespace);
        返回XSN;
      }
      组 { }
    }

    公共TestSoapHeaderTypeValuePair(字符串类型,字符串值){
      TYPE =类型;
      值=价值;
    }

    公共TestSoapHeaderTypeValuePair(){}

    [System.Xml.Serialization.XmlAttributeAttribute(类,命名空间=htt​​p://www.example.com/namespace)
    公共字符串类型{
      {返回_type; }
      集合{_type =价值; }
    }

    [System.Xml.Serialization.XmlText()]
    公共字符串值{
      得到{_value; }
      集合{_value =值; }
    }
  }
 

解决方案

的IXmlSerializable 可能?

请注意我还添加了(到 A ):

  [的XmlElement(A,命名空间=htt​​p://www.example.com/namespace)
公共TestSoapHeaderTypeValuePair A {...}
 

这里的code:

 公共类TestSoapHeaderTypeValuePair:的IXmlSerializable
{
    私人字符串_type;
    私人字符串_value;

    公共TestSoapHeaderTypeValuePair(字符串类型,字符串值)
    {
        TYPE =类型;
        值=价值;
    }

    公共TestSoapHeaderTypeValuePair(){}

    公共字符串类型
    {
        {返回_type; }
        集合{_type =价值; }
    }

    公共字符串值
    {
        得到{_value; }
        集合{_value =值; }
    }

    #地区的IXmlSerializable会员

    System.Xml.Schema.XmlSchema IXmlSerializable.GetSchema()
    {
        返回null;
    }

    无效IXmlSerializable.ReadXml(System.Xml.XmlReader阅读器)
    {
        抛出新的NotImplementedException();
    }

    无效IXmlSerializable.WriteXml(System.Xml.XmlWriter作家)
    {
        writer.WriteAttributeString(恩,型,http://www.example.com/namespace型);
        writer.WriteString(价值);
    }

    #endregion
}
 

This is what I get:

<ex:test soap:mustUnderstand="1" xmlns:ex="http://www.example.com/namespace">
  <ex:A Type="lorem">ipsum</ex:A>
</ex:test>

This is what I want: (Note that the Type-attribute is prefixed with ex.)

<ex:test soap:mustUnderstand="1" xmlns:ex="http://www.example.com/namespace">
  <ex:A ex:Type="lorem">ipsum</ex:A>
</ex:test>

This is my code:

  [XmlType(Namespace = "http://www.example.com/namespace")]
  [XmlRoot("ex", Namespace = "http://www.example.com/namespace")]
  public class TestSoapHeader : SoapHeader {
    private TestSoapHeaderTypeValuePair _a;

    public TestHeader() {
      MustUnderstand = true;
    }

    [XmlNamespaceDeclarations]
    public XmlSerializerNamespaces xmlsn {
      get {
        XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
        xsn.Add("ex", "http://www.example.com/namespace");
        return xsn;
      }
      set { }
    }

    public TestSoapHeaderTypeValuePair A {
      get { return _a; }
      set { _a = value; }
    }

  }

  [XmlType(Namespace = "http://www.example.com/namespace")]
  public class TestSoapHeaderTypeValuePair {
    private string _type;
    private string _value;

    [XmlNamespaceDeclarations]
    public XmlSerializerNamespaces xmlsn
    {
      get
      {
        XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
        xsn.Add("ex", "http://www.example.com/namespace");
        return xsn;
      }
      set { }
    }

    public TestSoapHeaderTypeValuePair(string type, string value) {
      Type = type;
      Value = value;
    }

    public TestSoapHeaderTypeValuePair() {}

    [System.Xml.Serialization.XmlAttributeAttribute("type", Namespace = "http://www.example.com/namespace")]
    public string Type {
      get { return _type; }
      set { _type = value; }
    }

    [System.Xml.Serialization.XmlText()]
    public string Value {
      get { return _value; }
      set { _value = value; }
    }
  }

解决方案

IXmlSerializable maybe?

Note I also added (to A):

[XmlElement("A", Namespace = "http://www.example.com/namespace")]
public TestSoapHeaderTypeValuePair A {...}

here's the code:

public class TestSoapHeaderTypeValuePair : IXmlSerializable
{
    private string _type;
    private string _value;

    public TestSoapHeaderTypeValuePair(string type, string value)
    {
        Type = type;
        Value = value;
    }

    public TestSoapHeaderTypeValuePair() { }

    public string Type
    {
        get { return _type; }
        set { _type = value; }
    }

    public string Value
    {
        get { return _value; }
        set { _value = value; }
    }

    #region IXmlSerializable Members

    System.Xml.Schema.XmlSchema IXmlSerializable.GetSchema()
    {
        return null;
    }

    void IXmlSerializable.ReadXml(System.Xml.XmlReader reader)
    {
        throw new NotImplementedException();
    }

    void IXmlSerializable.WriteXml(System.Xml.XmlWriter writer)
    {
        writer.WriteAttributeString("ex", "type", "http://www.example.com/namespace", Type);
        writer.WriteString(Value);
    }

    #endregion
}

这篇关于我如何XmlSerializer的名称空间添加到属性中嵌套的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆