这是我得到:如何让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; }
}
}
的既不你的例子是有效的XML,没有ex名称空间声明。 – 2009-12-09 14:42:36
你想要的是错的。我会建议谁写这个系统的另一方需要冗余的人的头。 – Will 2009-12-09 14:44:21
@Paul:对不起,我不想公开原来的名字,并将其替换为错误的。 – svinto 2009-12-09 14:59:27