Adding a Prefix to a Serial XML Element

I have the following class that implements 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 make a mistake, for example:

 throw new FaultException<ExceptionReport>(exceptions.GetExceptionReport(), new FaultReason("A server exception was encountered."), new FaultCode("Receiver")); 

I get the following XML in soap error details:

 ... <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 I really need the "ows" prefix also in the root element of the ExceptionReport:

 ... <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 to add this prefix?

+4
source share
1 answer

How to add a property to ExceptionReport and decorate it with XmlNamespaceDeclarationsAttribute , and also set the Namespace XmlRoot property in the ExceptionReport class as follows:

 ... [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 in namespaces in your constructor, or wherever you want, you need to have a property that returns the correct instance of XmlSerializerNamespaces .

0
source

All Articles