将XML序列化为C#类时出现错误

4

我正在尝试将XML文件序列化为C#对象,但是出现了以下错误:

XML文档中存在错误(1, 64)。

我复制了XML文件并在Visual Studio中使用“特殊粘贴为xml”选项粘贴。

这是我的XML:

<?xml version="1.0" encoding="Windows-1252" standalone="yes"?><root><record CodUnic="G15_455262_RO6739810_2016" CodStatie="G15" DocId="1" TipDoc="Gastro" NrDoc="455262" DataDoc="2016-01-21" DataIntr="2016-01-21" NrIntr="0" Retur="0" DataAnulare="" CodFurnizor="RO6739810" DenFurnizor="SC IFANTIS GGG SRL" ValoareFaraTVA="171.91" ValoareTVA="15.47" /></root>

这是我生成的“特殊类”:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XMLCheckTool.Clase
{

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class IntrCap
{

    private rootRecord recordField;

    /// <remarks/>
    public rootRecord record
    {
        get
        {
            return this.recordField;
        }
        set
        {
            this.recordField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class rootRecord
{

    private string codUnicField;

    private string codStatieField;

    private int docIdField;

    private string tipDocField;

    private uint nrDocField;

    private System.DateTime dataDocField;

    private System.DateTime dataIntrField;

    private int nrIntrField;

    private int returField;

    private string dataAnulareField;

    private string codFurnizorField;

    private string denFurnizorField;

    private decimal valoareFaraTVAField;

    private decimal valoareTVAField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string CodUnic
    {
        get
        {
            return this.codUnicField;
        }
        set
        {
            this.codUnicField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string CodStatie
    {
        get
        {
            return this.codStatieField;
        }
        set
        {
            this.codStatieField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public int DocId
    {
        get
        {
            return this.docIdField;
        }
        set
        {
            this.docIdField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string TipDoc
    {
        get
        {
            return this.tipDocField;
        }
        set
        {
            this.tipDocField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public uint NrDoc
    {
        get
        {
            return this.nrDocField;
        }
        set
        {
            this.nrDocField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute(DataType = "date")]
    public System.DateTime DataDoc
    {
        get
        {
            return this.dataDocField;
        }
        set
        {
            this.dataDocField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute(DataType = "date")]
    public System.DateTime DataIntr
    {
        get
        {
            return this.dataIntrField;
        }
        set
        {
            this.dataIntrField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public int NrIntr
    {
        get
        {
            return this.nrIntrField;
        }
        set
        {
            this.nrIntrField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public int Retur
    {
        get
        {
            return this.returField;
        }
        set
        {
            this.returField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string DataAnulare
    {
        get
        {
            return this.dataAnulareField;
        }
        set
        {
            this.dataAnulareField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string CodFurnizor
    {
        get
        {
            return this.codFurnizorField;
        }
        set
        {
            this.codFurnizorField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string DenFurnizor
    {
        get
        {
            return this.denFurnizorField;
        }
        set
        {
            this.denFurnizorField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public decimal ValoareFaraTVA
    {
        get
        {
            return this.valoareFaraTVAField;
        }
        set
        {
            this.valoareFaraTVAField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public decimal ValoareTVA
    {
        get
        {
            return this.valoareTVAField;
        }
        set
        {
            this.valoareTVAField = value;
        }
    }
}


}

这是将XML转换为对象的方法:

 public static void fileCapIntr(string xmlFile) {
        XmlSerializer serializer = new XmlSerializer(typeof(IntrCap));
        IntrCap i;
        using (Stream reader = new FileStream(xmlFile, FileMode.Open))
        {
            // Call the Deserialize method to restore the object's state.
            i = (IntrCap)serializer.Deserialize(reader);
        }


    }

请问有什么可以帮您的吗? 非常感谢!


请展示给我们XML文档。 - STF
我帖子中的第一个“code”部分是XML文档。 - Michael Commons
@STF 我撤销了你的编辑,因为当XML格式不同的时候,错误消息中提到的位置将没有意义。 - Manfred Radlwimmer
@ManfredRadlwimmer,但是这样你就可以看到XML文件中是否存在问题 - 这种情况经常发生。 - STF
@STF 我同意,通常来说,正确缩进XML是一个好主意,但在这种情况下,它会成为障碍。不要因此而放弃格式化问题,但请始终考虑上下文。 - Manfred Radlwimmer
有人能帮我解决这个问题吗?https://stackoverflow.com/questions/72265860/invaliddatacontractexception-type-cannot-be-serialized-consider-marking-it-wit - Ken
1个回答

4

你的错误

XML文档中有一个错误(1, 64)。

有一个InnerException,内容如下:

<root xmlns=''> was not expected.

简单的解决方法:指定root元素的名称:

[System.Xml.Serialization.XmlRootAttribute(
    Namespace = "", 
    IsNullable = false, 
    ElementName = "root"
 )]

或者在创建序列化程序时指定它

XmlSerializer serializer = new XmlSerializer(
    typeof(IntrCap), 
    null, 
    null, 
    new XmlRootAttribute("root"),
    ""
);

Proof as dotNetFiddle


动态XML文件怎么样?我的意思是根节点的数量是动态的。 - Michael Commons
你不能有多个根节点(实际的根节点,而不是名为“root”的元素)。如果你有多个root元素,它们需要被封装在另一个元素中,例如roots。在这种情况下,你可以通过在集合上定义XmlArrayXmlArrayItem属性或创建一个typeof(List<IntrCap>)XmlSerializer来指定元素的名称,但这取决于你的xml具体是什么样子。如果其他方法都失败了,你总可以使用XmlReaderXmlDocument手动解析你的xml。 - Manfred Radlwimmer
请问在以下XML中的(1,292)位置出现的错误是什么:https://jsfiddle.net/ufcd879u/1/ - Michael Commons
你为什么删除了另一个问题? - Manfred Radlwimmer
我解决了它,但是每个人都给我评了一个“负号” :) - Michael Commons

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接