使用替换组是无效的XML从XSD.EXE序列化产生的code(无效的xsi:type错误) [英] XML Serialized from xsd.exe generated code using substitution groups is invalid (invalid xsi:type error)
问题描述
我已经产生一些3GPP的XSD一些C#类(多个XSD文件/命名空间),它的伟大工程的序列化除了一个问题,在置换组中的一个抽象类型的具体实例。
I've generated some C# classes from some 3GPP XSDs (multiple XSD files/namespaces) and it works great for serialization apart from one problem with concrete instances of an abstract type used in a substitution group.
首先,模式的相关部分:
Firstly, the relevant parts of the schema:
(genericNrm.xsd)
(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)
(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
这是一个简单的ManagedElement所载ENBFunction序列化的XML是:
The XML serialised from a simple ManagedElement with contained ENBFunction is:
<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:typehttp://www.3gpp.org/ftp/specs/archive/32_series/32.765#eutranNrm:ENBFunction。
The built in visual studio XML validation complains about the element, stating "This is an invalid xsi:type 'http://www.3gpp.org/ftp/specs/archive/32_series/32.765#eutranNrm:ENBFunction'.
原来是序列化的XML错误或验证?它是什么做的不同的命名空间?
So is the serialized XML wrong or the validation? Is it something to do with the separate namespaces?
我可以反序列化的XML得很好,但我需要生成的XML是架构标准(不改变模式)。我发现,如果我手动的XML更改为以下,错误消失(我觉得更容易也读):
I can deserialize the XML just fine but I need the generated XML to be schema compliant (without changing the schemas). I found that if I manually change the XML to the following, the error disappears (and I find it easier to read also):
<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>
我可以强制串行输出这种方式?
Can I force the serializer to output it this way?
感谢您看......
Thanks for looking...
推荐答案
我通过手工编辑code从XSD生成的解决了这个。一个XmlElementAttribute需要在ManagedElement类项目集合,以确保序列化可以正常工作:
I solved this by manually editing the code generated from the XSD. An XmlElementAttribute is needed on the ManagedElement classes Items collection to ensure the serialisation works correctly:
[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继承的所有类,以确保正确的是用在序列化的时间。
This attribute is required for all classes inherited from ManagedElement to ensure the correct one is used at serialization time.
这篇关于使用替换组是无效的XML从XSD.EXE序列化产生的code(无效的xsi:type错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!