views:

230

answers:

1

Hi,

This is what I get:

<ex:test soap:mustUnderstand="1" xmlns:ex="http://www.example.com/namespace"&gt;
  <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"&gt;
  <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; }
    }
  }
+1  A: 

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
}
Marc Gravell