如何让 XMLSerializer 向嵌套对象中的属性添加命名空间?
- 作者: 咖啡泡饭007
- 来源: 51数据库
- 2022-10-28
问题描述
这是我得到的:
<ex:test soap:mustUnderstand="1" xmlns:ex="http://www.example.com/namespace"> <ex:A Type="lorem">ipsum</ex:A> </ex:test>
这就是我想要的:(请注意,Type-attribute 以 ex 为前缀.)
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>
这是我的代码:
[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; }
}
}
推荐答案
IXmlSerializable 也许?
注意我还添加了(到A):
Note I also added (to 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
}
推荐阅读
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
热点文章
团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
0
使用 MSBuild.exe 在发布模式下构建 C# 解决方案
0
当我发布 Web 应用程序时,AfterPublish 脚本不运行
0
构建时 T4 转换的产品仅在下一个构建中使用
0
ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
0
新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
0
如何将条件编译符号(DefineConstants)传递给 msbuild
0
MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
0
NuGet 包还原找不到包,没有源
0
使用 C# 6.0 功能运行 TFS 构建
0
