Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何解决反序列化xml文件时产生的问题?_C#_Xml_Windows Phone 8_Deserialization - Fatal编程技术网

C# 如何解决反序列化xml文件时产生的问题?

C# 如何解决反序列化xml文件时产生的问题?,c#,xml,windows-phone-8,deserialization,C#,Xml,Windows Phone 8,Deserialization,反序列化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.

反序列化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>
帮助我解决问题

  • 表示必须将符号转义到&或类似。没关系

  • 显然出了点问题。也许是你的XML。因此,删除它的主要部分,并找出导致错误的节点

  • 也许这是你的职业定义。因此,请使用XDocument.Load或.Parse来确定XML是否正确,并知道您的类定义存在错误。通过删除类中的部分来简化该类,并找出导致错误的字段/属性/属性


问题在于Xml文件中的空节点值。我已将反序列化类的属性修改为可空。它工作正常。

&
不是有效的xml实体(它实际上是一个html实体)。如何生成xml文件?您必须在反序列化xml之前更正它。如果可能,在源位置。@SteveB:不是预定义的XML实体吗?有5个预定义实体:
&
&apos
MSXML表示XML格式良好。我认为实体没有问题。我认为WP8有问题。现在我已经测试过,相同的类和去搜索功能在另一台机器上的WP7应用程序中工作。WP8有什么我缺少的吗?@ThomasW。你是对的:。”也许“答案和假设没有多大帮助。正如我在评论中所写,XML根据MSXML格式良好。我仍然不明白为什么应用程序针对Windows Phone 7时不提出这个问题。此外,我在两个应用程序中引用相同的名称空间进行反序列化。无论如何,它与WP8一起工作,属性的数据类型为空
[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;}
    }
}