Robel Haile Robel Haile - 3 months ago 21
C# Question

Weird error when i try to Deserialize a xml string to a .net List Object

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>


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;
}
}
}


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

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)")

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

Answer

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
}

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