[英]Adding prefix to serialized XML element
I have the following class which implements IXmlSerializable
: 我有以下类实现
IXmlSerializable
:
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Xml.Serialization.XmlSchemaProviderAttribute("ExportSchema")]
[System.Xml.Serialization.XmlRootAttribute(IsNullable = false)]
public partial class ExceptionReport : object, System.Xml.Serialization.IXmlSerializable
{
private System.Xml.XmlNode[] nodesField;
private static System.Xml.XmlQualifiedName typeName = new System.Xml.XmlQualifiedName("ExceptionReport", "http://www.opengis.net/ows");
public System.Xml.XmlNode[] Nodes
{
get
{
return this.nodesField;
}
set
{
this.nodesField = value;
}
}
public void ReadXml(System.Xml.XmlReader reader)
{
this.nodesField = System.Runtime.Serialization.XmlSerializableServices.ReadNodes(reader);
}
public void WriteXml(System.Xml.XmlWriter writer)
{
System.Runtime.Serialization.XmlSerializableServices.WriteNodes(writer, this.Nodes);
}
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public static System.Xml.XmlQualifiedName ExportSchema(System.Xml.Schema.XmlSchemaSet schemas)
{
System.Runtime.Serialization.XmlSerializableServices.AddDefaultSchema(schemas, typeName);
return typeName;
}
}
When i throw an error like this: 当我抛出这样的错误:
throw new FaultException<ExceptionReport>(exceptions.GetExceptionReport(), new FaultReason("A server exception was encountered."), new FaultCode("Receiver"));
I get the following XML in the soap fault detail: 我在soap fault详细信息中获得以下XML:
...
<detail>
<ExceptionReport xmlns="http://www.opengis.net/ows">
<ows:Exception exceptionCode="NoApplicableCode" locator="somewhere" xmlns:ows="http://www.opengis.net/ows">
<ows:ExceptionText>mymessage</ows:ExceptionText>
</ows:Exception>
</ExceptionReport>
</detail>
...
But what i really want is the "ows" prefix also on the root ExceptionReport element: 但我真正想要的是根ExceptionReport元素上的“ows”前缀:
...
<detail>
<ows:ExceptionReport xmlns:ows="http://www.opengis.net/ows">
<ows:Exception exceptionCode="NoApplicableCode" locator="somewhere" xmlns:ows="http://www.opengis.net/ows">
<ows:ExceptionText>mymessage</ows:ExceptionText>
</ows:Exception>
</ows:ExceptionReport>
</detail>
...
How can i add that prefix? 我该如何添加该前缀?
How about adding a property to ExceptionReport
and decorating it with XmlNamespaceDeclarationsAttribute , and also setting the Namespace
property of XmlRoot
attribute on ExceptionReport
class like this: 如何向
ExceptionReport
添加属性并使用XmlNamespaceDeclarationsAttribute进行装饰,以及在ExceptionReport
类上设置XmlRoot
属性的Namespace
属性,如下所示:
...
[XmlRoot(IsNullable = false, Namespace = "http://www.opengis.net/ows")]
public partial class ExceptionReport
{
[XmlNamespaceDeclarations()]
public XmlSerializerNamespaces xmlns
{
get
{
XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
xmlns.Add("ows", "http://www.opengis.net/ows");
return xmlns;
}
set
{
}
}
...
}
Of course you can fill the namespaces in your constructor or wherever you'd like, the point is to have a property which returns the proper XmlSerializerNamespaces
instance. 当然,您可以在构造函数中或任何您想要的位置填充命名空间,关键是要有一个返回正确的
XmlSerializerNamespaces
实例的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.