Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

c# - How do I get the XMLSerializer to add namespaces to attributes in nested objects?

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; }
    }
  }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

e.g. xsd.exe complex.xml xsd.exe gen1.xsd gen2.xsd /c

no need to touch xsd generated files (except to replace [][] -> [])

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("dfs", "http://schemas.microsoft.com/office/infopath/2003/dataFormSolution");
ns.Add("d", "http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields");
ns.Add("pc", "http://schemas.microsoft.com/office/infopath/2007/PartnerControls");

XmlSerializer serializer = new XmlSerializer(typeof(myFields));
StringBuilder sb = new StringBuilder();

using (StringWriter writer = new StringWriter(sb))
{
    serializer.Serialize(writer, myFields, ns);

    return sb.ToString();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...