2010-01-27 64 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的具有一個非空體創建的頂層對象內部的。