【问题标题】:Adding prefix to serialized XML element为序列化的 XML 元素添加前缀
【发布时间】:2023-04-02 22:00:01
【问题描述】:

我有以下实现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;
    }
}

当我抛出这样的错误时:

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

我在肥皂故障详细信息中得到以下 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>
...

但我真正想要的是根 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>
...

如何添加该前缀?

【问题讨论】:

  • 第二个 XML 示例是错误的。它在定义它之前使用前缀。
  • @JohnSaunders:已修复,谢谢!
  • 好。另请注意,这两个示例现在是相同的,因为前缀不相关 - 它只是命名空间的别名,现在所有元素的命名空间都相同。
  • @JohnSaunders:是的,这纯粹是为了审美价值。我还将从异常元素中删除 xmlns:ows 属性。并且知道该怎么做,但我首先需要将它添加到 ExceptionReport 元素中。 ://

标签: c# wcf soap xml-serialization


【解决方案1】:

如何将属性添加到ExceptionReport 并用XmlNamespaceDeclarationsAttribute 装饰它,同时在ExceptionReport 类上设置Namespace 属性的ExceptionReport 属性,如下所示:

...
[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
       { 
       }
   }

   ...
}

当然,您可以在构造函数或任何您喜欢的地方填充命名空间,关键是要有一个返回正确XmlSerializerNamespaces 实例的属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 2021-09-26
    • 1970-01-01
    相关资源
    最近更新 更多