将xml序列化为c#类时出错

时间:2016-05-25 06:45:50

标签: c# xml

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

  

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

我在Visual Studio中复制XML文件并将粘贴特殊为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);
        }


    }

请帮忙吗? 非常感谢!

1 个答案:

答案 0 :(得分:3)

您的错误

  

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

InnerException内容为:

  

&lt; root xmlns =''&gt;没想到。

简单修复:指定root的ElementName

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

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

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

Proof as dotNetFiddle