2009-12-09 158 views
3

这是我得到:如何让XMLSerializer将名称空间添加到嵌套对象中的属性?

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

这就是我想要的:(注意类型属性的前缀是前)

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

这是我的代码:

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

的既不你的例子是有效的XML,没有ex名称空间声明。 – 2009-12-09 14:42:36

+0

你想要的是错的。我会建议谁写这个系统的另一方需要冗余的人的头。 – Will 2009-12-09 14:44:21

+0

@Paul:对不起,我不想公开原来的名字,并将其替换为错误的。 – svinto 2009-12-09 14:59:27

回答

5

IXmlSerializable也许?

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

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

下面的代码:

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

例如XSD.EXE complex.xml XSD.EXE gen1.xsd gen2.xsd/C

无需触摸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(); 
} 
+0

Downvoted。这只会将它添加到根目录中。如果我正确理解它,问题会提到为特定的嵌套元素添加前缀/名称空间! – joedotnot 2017-10-11 08:51:47

相关问题