Xml反序列化异常

时间:2016-06-26 15:23:56

标签: c# xml

我正在尝试将xml文件反序列化为类。

这是我的xml文件:

<DirectoryListener inputDirectory="C:\test\" 
                   outputDirectory="C:\keyValueXml\" 
                   fileExt=".xml"/>

这是我的班级(由“特别粘贴”生成):

public class DirectoryListenConfig
{
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class DirectoryListener
    {
        private string inputDirectoryField;
        private string outputDirectoryField;
        private string fileExtField;

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string inputDirectory
        {
            get
            {
                return this.inputDirectoryField;
            }
            set
            {
                this.inputDirectoryField = value;
            }
        }

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string outputDirectory
        {
            get
            {
                return this.outputDirectoryField;
            }
            set
            {
                this.outputDirectoryField = value;
            }
        }

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string fileExt
        {
            get
            {
                return this.fileExtField;
            }
            set
            {
                this.fileExtField = value;
            }
        }
    }
}

我试图反序列化xml文件:

string path = "DirectoryConfig.xml";

XmlSerializer serializer = new XmlSerializer(typeof(DirectoryListener));

StreamReader reader = new StreamReader(path);
directoryConfig = (DirectoryListener)serializer.Deserialize(reader);//Throw an exception

得到例外

  

System.Xml.dll中发生未处理的“System.InvalidOperationException”类型的异常附加信息:XML文档中存在错误(2,2)。)。

有什么问题? :\

谢谢!

1 个答案:

答案 0 :(得分:1)

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"c:\temp\test.xml";

            XmlSerializer serializer = new XmlSerializer(typeof(DirectoryListener));

            StreamReader reader = new StreamReader(path);
            DirectoryListener directoryListener = (DirectoryListener)serializer.Deserialize(reader);//Throw an exception
        }
    }


    [XmlRoot("DirectoryListener")]
    public partial class DirectoryListener
    {
        private string inputDirectoryField;
        private string outputDirectoryField;
        private string fileExtField;

        [XmlAttribute("inputDirectory")]
        public string inputDirectory {get; set; }

        [XmlAttribute("outputDirectory")]
        public string outputDirectory { get; set; }
        [XmlAttribute("fileExt")]
        public string fileExt { get; set; }
    }


}