2014-01-20 30 views
0

以下問題反序列化時,XML文件如何解決在反序列化xml文件中生成的問題?

{System.InvalidOperationException: There is an error in XML document (3, 514). ---> System.NotSupportedException: XLinq 
    at System.Xml.Serialization.XmlSerializationReader.ReadXmlNodes(Boolean elementCanBeType) 
    at System.Xml.Serialization.XmlSerializationReader.ReadTypedPrimitive(XmlQualifiedName type, Boolean elementCanBeType) 
    at System.Xml.Serialization.XmlSerializationReader.ReadTypedPrimitive(XmlQualifiedName type) 

這是XML,我試圖解析,

<ns0:Response xmlns:ns0="urn:ae:testwebsite:uniqueness:genericcontentsrs:1"> 
    <GenericContents> 
    <ModuleId>1296</ModuleId> 
    <Title>Getting around</Title> 
    <Description> 
     Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, &lt;a target="_blank" href="http://google.com"&gt;google.com&lt;/a&gt;, Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,&lt;br /&gt; 
     &lt;br /&gt;Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, TEst&amp;rsquo;s Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. 
    </Description> 
    <BuildingId>0</BuildingId> 
    <GeoCoordinateX></GeoCoordinateX> 
    <GeoCoordinateY></GeoCoordinateY> 
    <MainImage>http://testwebsite.com/testimage.jpg</MainImage> 
    <PublicLink>http://testwebsite.com/testpage.aspx</PublicLink> 
    <Subpages> 
     <Page> 
     <SSId>10</SSId> 
     <Title>Tours &amp; tour operators</Title> 
     <Image>http://testwebsite.com/testimage.jpg</Image> 
     <ModuleUniqueName>tours.tour.operators</ModuleUniqueName> 
     </Page> 
     <Page> 
     <SSId>7</SSId> 
     <Title>Taxis</Title> 
     <Image>http://testwebsite.com/testimage.jpg</Image> 
     <ModuleUniqueName>taxis</ModuleUniqueName> 
     </Page> 
    </Subpages> 
    <RelatedEntities> 
     <Entity> 
     <Id>36694</Id> 
     <Type>Image</Type> 
     <TypeDescription>Corniche Road</TypeDescription> 
     <URL>http://testwebsite.com/testimage.jpg</URL> 
     <Title>DataFolder/TestImage.jpg</Title> 
     </Entity> 
    </RelatedEntities> 
    </GenericContents> 
</ns0:Response> 

而且使用下面的類來反序列化,

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:ae:testwebsite:uniqueness:genericcontentsrs:1")] 
[System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:ae:testwebsite:uniqueness:genericcontentsrs:1", IsNullable = false)] 
public partial class Response 
{ 
    private GenericContents genericContentsField;  
    [System.Xml.Serialization.XmlElementAttribute(Namespace = "")] 
    public GenericContents GenericContents 
    { 
     get{return this.genericContentsField;} 
     set{this.genericContentsField = value;} 
    } 
} 

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)] 
public partial class GenericContents 
{ 
    private ushort moduleIdField; 
    private string titleField; 
    private string descriptionField; 
    private byte buildingIdField; 
    private object geoCoordinateXField; 
    private object geoCoordinateYField; 
    private string mainImageField; 
    private string publicLinkField; 
    private GenericContentsPage[] subpagesField; 
    private GenericContentsRelatedEntities relatedEntitiesField;  
    public ushort ModuleId 
    { 
     get{return this.moduleIdField;} 
     set{this.moduleIdField = value;} 
    }  
    public string Title 
    { 
     get{return this.titleField;} 
     set{this.titleField = value;} 
    }  
    public string Description 
    { 
     get{return this.descriptionField;} 
     set{this.descriptionField = value;} 
    } 
    public byte BuildingId 
    { 
     get{return this.buildingIdField;} 
     set{this.buildingIdField = value;} 
    }  
    public object GeoCoordinateX 
    { 
     get{return this.geoCoordinateXField;} 
     set{this.geoCoordinateXField = value;} 
    }   
    public object GeoCoordinateY 
    { 
     get{return this.geoCoordinateYField;} 
     set{this.geoCoordinateYField = value;} 
    } 
    public string MainImage 
    { 
     get{return this.mainImageField;} 
     set{this.mainImageField = value;} 
    }   
    public string PublicLink 
    { 
     get{return this.publicLinkField;} 
     set{this.publicLinkField = value;} 
    } 

    [System.Xml.Serialization.XmlArrayItemAttribute("Page", IsNullable = false)] 
    public GenericContentsPage[] Subpages 
    { 
     get{return this.subpagesField;} 
     set{this.subpagesField = value;} 
    } 

    public GenericContentsRelatedEntities RelatedEntities 
    { 
     get{return this.relatedEntitiesField;} 
     set{this.relatedEntitiesField = value;} 
    } 
} 


[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class GenericContentsPage 
{ 
    private ushort sSIdField; 
    private string titleField; 
    private string imageField; 
    private string moduleUniqueNameField; 
    public ushort SSId 
    { 
     get{return this.sSIdField;} 
     set{this.sSIdField = value;} 
    } 
    public string Title 
    { 
     get{return this.titleField;} 
     set{this.titleField = value;} 
    } 

    public string Image 
    { 
     get{return this.imageField;} 
     set{this.imageField = value;} 
    } 

    public string ModuleUniqueName 
    { 
     get{return this.moduleUniqueNameField;} 
     set{this.moduleUniqueNameField = value;} 
    } 
} 

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class GenericContentsRelatedEntities 
{ 
    private GenericContentsRelatedEntitiesEntity entityField; 
    public GenericContentsRelatedEntitiesEntity Entity 
    { 
     get{return this.entityField;} 
     set{this.entityField = value;} 
    } 
} 

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class GenericContentsRelatedEntitiesEntity 
{ 
    private ushort idField; 
    private string typeField; 
    private string typeDescriptionField; 
    private string uRLField; 
    private string titleField; 
    public ushort Id 
    { 
     get{return this.idField;} 
     set{this.idField = value;} 
    } 
    public string Type 
    { 
     get{return this.typeField;} 
     set{this.typeField = value;} 
    }  
    public string TypeDescription 
    { 
     get{return this.typeDescriptionField;} 
     set{this.typeDescriptionField = value;} 
    }  
    public string URL 
    { 
     get{return this.uRLField;} 
     set{this.uRLField = value;} 
    } 
    public string Title 
    { 
     get{return this.titleField;} 
     set{this.titleField = value;} 
    } 
} 

幫助發生我解決問題

+3

'&'不是有效的xml實體(它實際上是一個html實體)。你如何生成XML文件?在反序列化之前,您必須更正xml。如果可能的話在源頭。 –

+0

@SteveB:不是預定義的XML實體?有5個預定義的實體:「<」,「>」,「&」,「'」和「"」http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Predefined_entities_in_XML MSXML表示XML格式良好。 –

+1

我認爲沒有任何問題與entities.i認爲與WP8問題。現在我已經測試,相同的類和desearilizing與WP7應用程序在另一臺機器上工作。是否有任何我缺少WP8? –

回答

0

問題在於Xml文件中的空節點值。我已經將反序列化類的屬性修改爲Nullable.It工作正常。

+0

我仍然沒有得到爲什麼當針對Windows Phone 7的應用程序不會引發此問題。另外,我指的是在兩個應用程序中反序列化的相同命名空間。任何方式,它與WP8的Nullable數據類型的屬性 –

-1
  • http://www.w3.org/TR/2000/REC-xml-20001006#syntax表示「&」字符必須轉義爲&或類似內容。所以沒關係。

  • 顯然有些問題。也許你的XML。所以刪除它的主要部分,找出哪個節點導致錯誤。

  • 也許這是您的班級定義。因此,使用XDocument.Load或.Parse找出XML是否正確,並知道您的類定義有錯誤。通過從中刪除部分來簡化該類,並找出哪個字段/屬性/屬性/會導致錯誤。

+0

「也許」答案和假設不是很有幫助。正如我在我的評論中已經寫過的那樣,根據MSXML,XML格式良好。 –