C# 如何让 XMLSerializer 向嵌套对象中的属性添加命名空间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/1874295/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the XMLSerializer to add namespaces to attributes in nested objects?
提问by svinto
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 为前缀。)
<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; }
    }
  }
采纳答案by Marc Gravell
IXmlSerializablemaybe?
IXmlSerializable也许?
Note I also added (to A):
注意我还添加了(到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
}
回答by neuralsea
e.g. xsd.exe complex.xml xsd.exe gen1.xsd gen2.xsd /c
例如 xsd.exe complex.xml xsd.exe gen1.xsd gen2.xsd /c
no need to touch xsd generated files (except to replace [][] -> [])
无需触摸 xsd 生成的文件(除了替换 [][] -> [])
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();
}

