2010-01-27 65 views
2

我一直在尝试反序列化xsd.exe中的架构生成的类中的xml文件。不幸的是,只有一部分文件被正确地反序列化,其余的都是空的,原因是我无法解决。xmlserializer没有正确反序列化导入架构

我的过程如下:

<?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"> 

: 与从其生成C#代码的myschema.xsd文件起始

和进口parentschema.xsd文件是这样:

<?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:

>xsd.exe /c myschema.xsd parentschema.xsd 

生成一个myschema_parentschema.cs文件

要测试它,我试图反序列化一个示例xml文件:

<?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的代码,其中阅读器是上述XML文件的XmlReader:

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元素?

+0

交叉发布到http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/256c43ab-5206-491f-b77e-42903b8c616a – 2010-05-04 07:37:03

+0

本文中遇到的类似问题:[http:// richnewman.wordpress.com/2008/01/28/problems-with-using-xsdexe-to-generate-net-classes-from-the-fpml-xsd-schema-introduction-to-using-fpml-with-net-工具 - 部分4 /] – 2010-05-10 20:08:49

+0

myschema.xsd看起来不完整... – code4life 2010-10-14 18:58:59

回答

0

您的parentschema.xsd中存在拼写错误。你过早地关闭<xs:element>标签body标签:

<xs:element name="body" type="mc:body" abstract="true"/> 

也可以定义身体抽象我认为这是一个错误(如果我读XML正确)。

全高清(基于您的XML)应该是这个样子:

<xs:element name="body" type="mc:body" abstract="true"> 
    <xs:complexType> 
     <xs:attribute name="id" type="xs:ID" use="required"/> 
     <xs:sequence> 
      <xs:element type="Foo" type="xs:anyURI" /> 
     </xs:sequence> 
    </xs:complexType> 
</xs:element> 
0

我遵循相同的步骤,你,它看起来像你的架构和你反序列化不是XML文件匹配。下面是我所做的:

static void Main(string[] args) 
{ 
    var xs = new XmlSerializer(typeof(toplevel)); 

    // test saving to xml first... 
    var tl = new toplevel(); 
    tl.toplevel_header = new toplevel_header(); 
    tl.toplevel_header.name = "MyName"; 

    tl.body = new body(); 
    tl.body.id = "body id..."; 

    // output to console first... 
    var cw = Console.OpenStandardOutput(); 
    xs.Serialize(cw, tl); 
    Console.WriteLine(); 
    Console.WriteLine(); 

    // save to file... 
    var fw = File.CreateText("test.xml"); 
    xs.Serialize(fw, tl); 
    fw.Close(); 

    // read file... 
    var fr = File.Open("test.xml", FileMode.Open, FileAccess.Read); 
    var obj = xs.Deserialize(fr); 
    var fooBar = (toplevel)obj; 

    Console.WriteLine(fooBar.toplevel_header.name); 
    Console.WriteLine(fooBar.body.id); 
    Console.ReadLine(); 
} 

是串行生成的XML是这样的:

<?xml version="1.0" encoding="utf-8"?> 
<toplevel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns="myschema:common"> 
    <toplevel_header> 
    <name>MyName</name> 
    </toplevel_header> 
    <body id="body id..." /> 
</toplevel> 

的XML显然不符合您所使用的输入XML文件...希望这可以帮助你走好人生办法!

0

看你的示例XML我注意到一个不一致这就是为什么XmlSerializer的不与你的期望上来的原因:你定义的xmlns =

<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> 

在你的顶层元素“MYSCHEMA:常见的” ,然而在你的body元素中你定义了xmlns =“http://www.myuri.org/schema”,下一行是xmlns:mc =“myschema:common”。这意味着正文中的Foo元素位于不同的名称空间下,并且XmlSerializer不会找到该元素。当我删除在所述主体元件的xmlns声明和改变了的xmlns:MC声明的xmlns,像这样:

<?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> 

已经调整了示例XML指示XmlSerializer的具有一个非空体创建的顶层对象内部的。