0
我有問題將xml反序列化到多個類對象。當我嘗試喲反序列化 我越來越「沒有預料到」。反序列化錯誤:「<book xmlns ='>不是預期的。」從xml字符串DeSerialize
這裏是我的調用代碼
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;
}
}
}
謝謝Dipesh,它像寶石一樣工作。 :) – 2012-03-01 09:16:47