反序列化XML InvalidOperationException

时间:2016-05-11 18:51:03

标签: c# xml serialization

我试图通过本指南反序列化xml文件:http://www.codeproject.com/Articles/487571/XML-Serialization-and-Deserialization-Part-2

但是当我运行app时,我收到以下异常:InvalidOperationException - XML文档中的错误(2,2)

static void Main(string[] args)
{
   XmlSerializer deserializer = new XmlSerializer(typeof(Address));
   TextReader reader = new StreamReader(@"D:\data.xml");
   object obj = deserializer.Deserialize(reader);
   Address XmlData = (Address)obj;
   reader.Close();
}
public class Address
{
   public int HouseNo { get; set; }
   public string StreetName { get; set; }
   public string City { get; set; }
}

这是我的XML:

<Address>
    <HouseNo>4</HouseNo>
    <StreetName>Rohini</StreetName>
    <City>Delhi</City>
</Address>

1 个答案:

答案 0 :(得分:0)

我猜是因为你已经把你的&#34;地址&#34;课内&#34;课程&#34;像这样的课:

class Program
{
    static void Main(string[] args){...}
    class Address{...}
}

解决这个问题只需移动你的地址&#34;超出&#34;计划&#34;像这样:

class Program
{
    static void Main(string[] args){...}
}
class Address{...}