私はこの質問を2回は既に行っていますが、実際にはうまくいきません。xml文字列を.netリストオブジェクトにデシリアライズしようとすると奇妙なエラーが発生する
<?xml version="1.0" encoding="UTF-8"?>
<people type="array">
<person>
<first-name>Mark</first-name>
<last-name>Zette</last-name>
<e-mail>[email protected]</e-mail>
</person>
<person>
<first-name>Sara</first-name>
<last-name>Brobro</last-name>
<e-mail>[email protected]</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>[email protected]</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;
}
}
}
私はこのXMLを持っています。ビジュアルスタジオのコマンドプロンプトで "xsd.exe peoples.xml"を実行してから "xsd.exe/c peoples.xsd"を実行してください。
さて、これは私のコードプラス誤差である:(と追加情報の後にスウェーデン語を知らないもののために、それは「あなたのXMLドキュメント(3,2)と間違って何かがある」と言う)
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と生成されたコードを最小限の例に減らしてください - [mcve] –
Okej done。あなたは今私を助けてください:) @JonSkeet –