2

我已经从一些 3GPP XSD(多个 XSD 文件/命名空间)生成了一些 C# 类,除了替换组中使用的抽象类型的具体实例的一个问题之外,它非常适合序列化。

首先,架构的相关部分:

(genericNrm.xsd)

<element name="ManagedElement">
    <complexType>
      <complexContent>
        <extension base="xn:NrmClass">
          <sequence>
            ...
            <choice minOccurs="0" maxOccurs="unbounded">
              <element ref="xn:IRPAgent"/>
              <element ref="xn:ManagedElementOptionallyContainedNrmClass"/>
              <element ref="xn:VsDataContainer"/>
            </choice>
          </sequence>
        </extension>
      </complexContent>
    </complexType>
</element>

<element
    name="ManagedElementOptionallyContainedNrmClass"
    type="xn:NrmClass"
    abstract="true"
/>

(eutran.xsd)

<element name="ENBFunction" substitutionGroup="xn:ManagedElementOptionallyContainedNrmClass">
    <complexType>
      <complexContent>
        <extension base="xn:NrmClass">
          <sequence>
            <element name="attributes" minOccurs="0">
              <complexType>
                <all>
                  <element name="userLabel" type="string" minOccurs="0"/>
... etc

从包含 ENBFunction 的简单 ManagedElement 序列化的 XML 是:

<ManagedElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="1234" xmlns="http://www.3gpp.org/ftp/specs/archive/32_series/32.625#genericNrm">
    <ManagedElementOptionallyContainedNrmClass xmlns:q1="http://www.3gpp.org/ftp/specs/archive/32_series/32.765#eutranNrm" xsi:type="q1:ENBFunction" id="1234">
      <q1:attributes>
        <q1:userLabel>label</q1:userLabel>
      </q1:attributes>
    </ManagedElementOptionallyContainedNrmClass>
  </ManagedElement>

内置的 Visual Studio XML 验证会抱怨该元素,指出“这是一个无效的 xsi:type 'http://www.3gpp.org/ftp/specs/archive/32_series/32.765#eutranNrm:ENBFunction'。

那么是序列化的 XML 错误还是验证错误?它与单独的名称空间有关吗?

我可以很好地反序列化 XML,但我需要生成的 XML 符合模式(不更改模式)。我发现如果我手动将 XML 更改为以下内容,错误就会消失(而且我发现它也更容易阅读):

<ManagedElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="1234" xmlns="http://www.3gpp.org/ftp/specs/archive/32_series/32.625#genericNrm">
    <q1:ENBFunction xmlns:q1="http://www.3gpp.org/ftp/specs/archive/32_series/32.765#eutranNrm" id="1234">
      <q1:attributes>
        <q1:userLabel>label</q1:userLabel>
      </q1:attributes>
    </q1:ENBFunction>
</ManagedElement>

我可以强制序列化器以这种方式输出吗?

谢谢你看...

4

1 回答 1

2

我通过手动编辑从 XSD 生成的代码解决了这个问题。ManagedElement 类 Items 集合需要 XmlElementAttribute 以确保序列化正常工作:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.3gpp.org/ftp/specs/archive/32_series/32.625#genericNrm")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://www.3gpp.org/ftp/specs/archive/32_series/32.625#genericNrm", IsNullable=false)]
public partial class ManagedElement : NrmClass {

...

[System.Xml.Serialization.XmlElementAttribute("ENBFunction", typeof(ENBFunction), Namespace = "http://www.3gpp.org/ftp/specs/archive/32_series/32.765#eutranNrm")]
public NrmClass[] Items {
...

从 ManagedElement 继承的所有类都需要此属性,以确保在序列化时使用正确的类。

于 2011-08-02T11:02:53.517 回答