簡體   English   中英

如何解決反序列化xml文件時產生的問題?

[英]How to resolve the issues generated in deserializing the xml file?

反序列化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;}
    }
}

幫我解決問題

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

  • http://www.w3.org/TR/2000/REC-xml-20001006#syntax指出,&符號必須轉義為&或類似符號。 所以沒關系。

  • 顯然有問題。 也許是您的XML。 因此,刪除它的主要部分,找出導致錯誤的節點。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM