当我尝试将xml字符串反序列化为.net列表对象时出现奇怪的错误 [英] Weird error when i try to Deserialize a xml string to a .net List Object

查看:86
本文介绍了当我尝试将xml字符串反序列化为.net列表对象时出现奇怪的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have asked this question twice already but not one have given me an answer that actually works so i will try again. I have this 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转换为该对象的列表:

So I want to turn this xml into a list of this object:

//------------------------------------------------------------------------------
// <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。

This class is autogenerated. Throug running "xsd.exe peoples.xml" and then "xsd.exe /c peoples.xsd" in the visual studio command prompt.

现在,这是我的代码加上错误:(对于那些在补充信息后不懂瑞典语的人,它会说您的XML文档(3,2)出了问题)

Now this is my code plus error: (And for those that don't know Swedish after additional information it says "There is something wrong with your XML-document(3,2)")

< a href = https://i.stack.imgur.com/EySoC.png rel = nofollow noreferrer>

 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();


推荐答案

您的XML文件不表示 List< people> -它包含一个单个 people ;这就是根元素。然后包含子元素。不过,您可以轻松获得它们:

Your XML file doesn't represent a List<people> - it contains a single people; that's what the root element is. That then contains sub-elements. You can get those easily though:

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命名约定并使用自动实现的属性。)

(I'd strongly advise you to rewrite the autogenerated code following .NET naming conventions and using automatically implemented properties though.)

这篇关于当我尝试将xml字符串反序列化为.net列表对象时出现奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆