反序列化错误:“<book xmlns =”“>不是预期的。”从xml字符串</book>中删除DeSerialize

时间:2012-03-01 07:25:49

标签: asp.net c#-4.0 xml-serialization xsd xml-parsing

我遇到了将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;
        }
    }

}

1 个答案:

答案 0 :(得分:2)

只需从Book类的属性中取消 Namespace = 即可。 所以你的代码就像

[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(IsNullable = false)]
    public partial class book