当我尝试将xml字符串反序列化为.net列表对象时出现奇怪的错误

时间:2016-08-16 19:10:35

标签: c# xml deserialization

我已经两次问过这个问题,但没有人给我一个实际有效的答案,所以我会再试一次。我有这个xml:

<?xml version="1.0" encoding="UTF-8"?>
<people type="array">
 <person>
       <first-name>Mark</first-name>
       <last-name>Zette</last-name>
       <e-mail>mark.zette@hotmail.com</e-mail>
 </person>
 <person>
       <first-name>Sara</first-name>
       <last-name>Brobro</last-name>
       <e-mail>brobro@hotmail.com</e-mail>
 </person>
 <person>
       <first-name>Rob</first-name>
       <last-name>Sel</last-name>
       <e-mail>rob.selhotmail.com</e-mail>
 </person>
 <person>
       <first-name>Aden</first-name>
       <last-name>Snor</last-name>
       <e-mail>aden.Snor@hotmail.com</e-mail>
 </person>
</people>

所以我想把这个xml变成这个对象的列表:

//------------------------------------------------------------------------------
// <auto-generated>
//     Denna kod har genererats av ett verktyg.
//     Körtidsversion:4.0.30319.42000
//
//     Ändringar i denna fil kan orsaka fel och kommer att förloras om
//     koden återgenereras.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=4.6.1055.0.
// 


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class people {

private peoplePerson[] personField;

private string typeField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("person", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public peoplePerson[] person {
    get {
        return this.personField;
    }
    set {
        this.personField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string type {
    get {
        return this.typeField;
    }
    set {
        this.typeField = value;
    }
}
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class peoplePerson {

private string firstnameField;

private string lastnameField;

private string emailField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("first-name", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string firstname {
    get {
        return this.firstnameField;
    }
    set {
        this.firstnameField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("last-name", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string lastname {
    get {
        return this.lastnameField;
    }
    set {
        this.lastnameField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("e-mail",    Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string email {
    get {
        return this.emailField;
    }
    set {
        this.emailField = value;
    }
}
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class NewDataSet {

private people[] itemsField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("people")]
public people[] Items {
    get {
        return this.itemsField;
    }
    set {
        this.itemsField = value;
    }
}
}

此类是自动生成的。在Visual Studio命令提示符下运行“xsd.exe peoples.xml”然后运行“xsd.exe / c peoples.xsd”。

现在这是我的代码加上错误:(对于那些不知道瑞典语的人,在附加信息之后它说“你的XML文档有问题(3,2)”)

enter image description here

 string xmlPath = Path.Combine(HostingEnvironment.MapPath("~/App_Data"), "peoples.xml");

            StreamReader sr = new StreamReader(xmlPath);
            string xml = sr.ReadToEnd(); //i suspect the problem might appear here maybe. When i convert the xml to string it might 
                                         //happend something to the xmlstring that the deserialize 
                                         //method cant read. But i dont know just guessing.

            XmlSerializer serializer = new XmlSerializer(typeof(List<people>));
            using (TextReader reader = new StringReader(xml))
            {
                List<people> person = (List<people>)serializer.Deserialize(reader);
            }

            return View();

1 个答案:

答案 0 :(得分:3)

您的XML文件不代表List<people> - 它包含 people;这是根元素是什么。然后包含子元素。你可以轻松地获得这些:

XmlSerializer serializer = new XmlSerializer(typeof(people));
using (TextReader reader = new StringReader(json))
{
    people person = (people) serializer.Deserialize(reader);
    List<peoplePerson> list = person.person.ToList();
    Console.WriteLine(list.Count); // Prints 4
}

(我强烈建议您按照.NET命名约定重写自动生成的代码,然后使用自动实现的属性。)