XmlSerializer的不正确的反序列化,拥有自营模式 [英] xmlserializer not correctly deserializing schema with import
问题描述
我一直在尝试反序列化在C#中的XML文件从XSD.EXE模式生成的类。不幸的是,文件只有部分得到了适当的反序列化,剩下的就是返回空的原因,我不能工作了。
I've been trying to deserialize an xml file in C# with classes generated from schemas in xsd.exe. Unfortunately only part of the file is being properly deserialized, the rest is returned as null for reasons I can't work out.
我的过程如下:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mc="myschema:common" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ttl="http://www.myuri.org/myschema" targetNamespace="http://www.myuri.org/myschema" elementFormDefault="qualified" attributeFormDefault="unqualified">
和进口parentschema.xsd文件是这样:
and the imported parentschema.xsd file is so:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:mc="myschema:common" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="myschema:common" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="toplevel">
<xs:complexType>
<xs:sequence>
<xs:element ref="mc:toplevel_header" minOccurs="0"/>
<xs:element ref="mc:body"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="toplevel_header">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:anyURI"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="body" type="mc:body" abstract="true"/>
<xs:complexType name="body">
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>
<xs:element name="Entity" type="mc:Entity" abstract="true"/>
<xs:complexType name="Entity" abstract="true">
<xs:attribute name="href" type="xs:anyURI" use="optional"/>
</xs:complexType>
</xs:schema>
我通过上述两种模式文件,以XSD.EXE:
I'm passing the two above schema files to xsd.exe:
>xsd.exe /c myschema.xsd parentschema.xsd
生成一个myschema_parentschema.cs文件
which generates a myschema_parentschema.cs file
要测试它,我尝试反序列化示例XML文件:
To test it I'm trying to deserialize a sample xml file:
<?xml version=\"1.0\" encoding="UTF-8"?>
<toplevel version="2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns="myschema:common"
xsi:schemaLocation="myschema:common http://www.myuri.org/parentschema.xsd">
<toplevel_header>
<name>MyName</name>
</toplevel_header>
<body id="body_1"
xmlns="http://www.myuri.org/schema"
xmlns:mc="myschema:common"
xsi:schemaLocation="http://www.myuri.org/myschema http://www.myuri.org/myschema.xsd">
<Foo href="http://www.google.com">
</Foo>
</body>
</toplevel>
而我传递给下面的XmlSerializer code,其中阅读器是一个XmlReader对于上述xml文件:
which I'm passing to the following XmlSerializer code, where reader is an XmlReader for the above xml file:
XmlSerializer xs = new XmlSerializer ( typeof ( toplevel ) );
object deserializedObject = xs.Deserialize( reader );
toplevel fooBar = (toplevel)deserializedObject;
Assert.AreEqual( "MyName", fooBar.toplevel_header.name ); //passes OK
Assert.IsNotNull( fooBar.body ); //<--------FAIL
为什么反序列化对象有一个空的身体特性,以及如何得到它正确地反序列化的Foo元素?
Why does the deserialized object have a null body property, and how do I get it to deserialize the Foo element properly?
推荐答案
看你的示例XML我注意到一个矛盾这就是为什么XmlSerializer的不来与你所期望的原因:
Looking at your sample xml I noticed a inconsistency which is the reason why the XmlSerializer is not coming up with what you expect:
<toplevel version="2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xlink="http://www.w3.org/1999/xlink"
***xmlns="myschema:common"***
xsi:schemaLocation="myschema:common http://www.myuri.org/parentschema.xsd">
<toplevel_header>
<name>MyName</name>
</toplevel_header>
<body id="body_1"
***xmlns="http://www.myuri.org/schema"***
***xmlns:mc="myschema:common"***
xsi:schemaLocation="http://www.myuri.org/myschema http://www.myuri.org/myschema.xsd">
<Foo href="http://www.google.com">
</Foo>
</body>
</toplevel>
在你的顶层元素正在定义的xmlns =MYSCHEMA:常见的,但在你的身体元素正在定义的xmlns =http://www.myuri.org/schema和下一行是的xmlns:MC =MYSCHEMA:常见的。这意味着,身体内部的富元件是在一个不同的命名空间和XmlSerializer的不会发现的元素。当我删除了body元素的xmlns声明,改变了的xmlns:MC声明的xmlns,像这样:
In your toplevel element you are defining xmlns="myschema:common", and yet in your body element you are defining xmlns="http://www.myuri.org/schema" and the next line is xmlns:mc="myschema:common". This means that the Foo element inside of the body is under a different namespace and the XmlSerializer will not find the element. When I deleted the xmlns declaration in the body element and changed the xmlns:mc declaration to xmlns, like so:
<?xml version="1.0" encoding="UTF-8"?>
<toplevel version="2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns="myschema:common"
xsi:schemaLocation="myschema:common http://www.myuri.org/parentschema.xsd">
<toplevel_header>
<name>MyName</name>
</toplevel_header>
<body id="body_1"
xmlns="myschema:common"
xsi:schemaLocation="http://www.myuri.org/myschema http://www.myuri.org/myschema.xsd">
<Foo href="http://www.google.com">
</Foo>
</body>
</toplevel>
已经指示XmlSerializer的创建了一个非空体的顶层对象在其内部调整的示例XML。
Having adjusted the sample xml as indicated the XmlSerializer created the toplevel object with a non-null body inside of it.
这篇关于XmlSerializer的不正确的反序列化,拥有自营模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!