У меня проблема с десериализацией xml для нескольких объектов класса.когда я пытаюсь выполнить десериализацию, я получаю «не ожидал».
Вот мой код вызова
StringReader strReader = new StringReader(xml);
XmlTextReader reader = new XmlTextReader(strReader);
reader.ReadToDescendant("book");
var temp = DeserializeFromXml<book>(reader.ReadOuterXml());
public static T DeserializeFromXml<T>(string xml)
{
T result;
XmlRootAttribute xRoot = new XmlRootAttribute();
XmlSerializer ser = new XmlSerializer(typeof(T));
using (TextReader tr = new StringReader(xml))
{
result = (T)ser.Deserialize(tr);
}
return result;
}
xml - это строковое сообщение
<?xml version="1.0" encoding="UTF-8"?><books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<book genre="novel">
<title>The Handmaid's Tale</title>
<price>19.95</price>
<ISBN>1-861003-78</ISBN>
<style>hardcover</style>
</book>
<library genre="novel">
<name>Oxford</name>
<location>london</location>
<ISBN>1-8888888-88</ISBN>
<address>12th main, chesmedia</address>
</library>
Когда я пытаюсь выполнить этот код, я получаю это внутреннее исключение "не ожидалось".
Вот мой код класса
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.233")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/SampleBooks.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://tempuri.org/SampleBooks.xsd", IsNullable = false)]
public partial class books
{
private book bookField;
private library libraryField;
private static System.Xml.Serialization.XmlSerializer serializer;
public books()
{
this.libraryField = new library();
this.bookField = new book();
}
[System.Xml.Serialization.XmlElementAttribute(Order = 0)]
public book book
{
get
{
return this.bookField;
}
set
{
this.bookField = value;
}
}
[System.Xml.Serialization.XmlElementAttribute(Order = 1)]
public library library
{
get
{
return this.libraryField;
}
set
{
this.libraryField = value;
}
}
private static System.Xml.Serialization.XmlSerializer Serializer
{
get
{
if ((serializer == null))
{
serializer = new System.Xml.Serialization.XmlSerializer(typeof(books));
}
return serializer;
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.233")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/SampleBooks.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://tempuri.org/SampleBooks.xsd", IsNullable = false)]
public partial class book
{
private title titleField;
private decimal priceField;
private ISBN iSBNField;
private style styleField;
private bookGenre genreField;
private static System.Xml.Serialization.XmlSerializer serializer;
[System.Xml.Serialization.XmlElementAttribute(Order = 0)]
public title title
{
get
{
return this.titleField;
}
set
{
this.titleField = value;
}
}
[System.Xml.Serialization.XmlElementAttribute(Order = 1)]
public decimal price
{
get
{
return this.priceField;
}
set
{
this.priceField = value;
}
}
[System.Xml.Serialization.XmlElementAttribute(Order = 2)]
public ISBN ISBN
{
get
{
return this.iSBNField;
}
set
{
this.iSBNField = value;
}
}
[System.Xml.Serialization.XmlElementAttribute(Order = 3)]
public style style
{
get
{
return this.styleField;
}
set
{
this.styleField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute()]
public bookGenre genre
{
get
{
return this.genreField;
}
set
{
this.genreField = value;
}
}
private static System.Xml.Serialization.XmlSerializer Serializer
{
get
{
if ((serializer == null))
{
serializer = new System.Xml.Serialization.XmlSerializer(typeof(book));
}
return serializer;
}
}
}