Ошибка преобразования десериализации Xml: существует ошибка в XML-документе (2, 2)
How do I Deserialize the following xml into object. I could not recognize the mistake that I am doing and I getting the "There is an error in XML document (2, 2)" error. Could you please help me on this.
Input Xml format i.e. books.xml
<?xml version="1.0" encoding="utf-8"?> <catalog xmlns="http://library.by/catalog" date="2016-02-05"> <book id="bk101"> <isbn>0-596-00103-7</isbn> <author>Löwy, Juval</author> <title>COM and .NET Component Services</title> <genre>Computer</genre> <publisher>O'Reilly</publisher> <publish_date>2001-09-01</publish_date> <description> COM & .NET Component Services provides both traditional COM programmers and new .NET component developers with the information they need to begin developing applications that take full advantage of COM+ services. This book focuses on COM+ services, including support for transactions, queued components, events, concurrency management, and security </description> <registration_date>2016-01-05</registration_date> </book> <book id="bk109"> <author>Kress, Peter</author> <title>Paradox Lost</title> <genre>Science Fiction</genre> <publisher>R & D</publisher> <publish_date>2000-11-02</publish_date> <description> After an inadvertant trip through a Heisenberg Uncertainty Device, James Salway discovers the problems of being quantum. </description> <registration_date>2016-01-05</registration_date> </book> </catalog>
Here are the classes:
using System; using System.Xml.Serialization; namespace Seralization { [Serializable()] public class Book { [XmlAttribute("id")] public string BookId { get; set; } [XmlElement("isbn")] public string Isbn { get; set; } [XmlElement("author")] public string Author { get; set; } [XmlElement("title")] public string Title { get; set; } [XmlElement("publisher")] public string Publisher { get; set; } [XmlElement("publish_date")] public DateTime PublishDate { get; set; } [XmlElement("description")] public string Description { get; set; } [XmlElement("registration_date")] public DateTime RegisteredDate { get; set; } [XmlIgnore] public Genre Genre; [XmlAttribute("genre")] public string GenreValue { get { return Genre.ToString(); } set { if (string.IsNullOrEmpty(value)) { Genre = default(Genre); } else { Genre = (Genre)Enum.Parse(typeof(Genre), value); } } } } }
using System; using System.Xml.Serialization; namespace Seralization { [Serializable()] [XmlRoot("catalog")] public class Calalog { [XmlAttribute] public DateTime createdDate; [XmlArray("catalog")] [XmlArrayItem("book", typeof(Book))] public Book[] Books { get; set; } } } using System.ComponentModel; namespace Seralization { public enum Genre { Computer = 1, Fantasy = 2, Romance = 3, Horror = 4, [Description("Science Fiction")] ScienceFiction = 5 } }
Что я уже пробовал:
using System; using System.IO; using System.Xml.Serialization; class Program { static void Main(string[] args) { Calalog cars = null; string path = @"C:\input\books.xml"; XmlSerializer serializer = new XmlSerializer(typeof(Calalog)); StreamReader reader = new StreamReader(path); cars = (Calalog)serializer.Deserialize(reader); Console.WriteLine(cars.Books.GetType()); reader.Close(); Console.ReadLine(); } }