Having issue Deserializing array from an XML string?

If so, you just need to add the XmlElement attribute to the Details member. This might not seem intuitive, but this tells the serializer that you want to serialize/deserialize the array items as elements rather than an array with the items as child elements of the array Here is a quick example public Test() { string xml = @" Test [email protected] 1 TestDetails 1 2 Testing 3 "; XmlSerializer xs = new XmlSerializer(typeof(DataSet)); DataSet ds = (DataSet)xs. Deserialize(new StringReader(xml)); } Serializable public class DataSet { public User User; } Serializable public class User { public string UserName { get; set; } public string Email { get; set; } XmlElement public Details Details { get; set; } } Serializable public class Details { public int ID { get; set; } public string Name { get; set; } public string Value { get; set; } }.

If so, you just need to add the XmlElement attribute to the Details member. This might not seem intuitive, but this tells the serializer that you want to serialize/deserialize the array items as elements rather than an array with the items as child elements of the array. Here is a quick example public Test() { string xml = @" Test test@test.

Com 1 TestDetails 1 2 Testing 3 "; XmlSerializer xs = new XmlSerializer(typeof(DataSet)); DataSet ds = (DataSet)xs. Deserialize(new StringReader(xml)); } Serializable public class DataSet { public User User; } Serializable public class User { public string UserName { get; set; } public string Email { get; set; } XmlElement public Details Details { get; set; } } Serializable public class Details { public int ID { get; set; } public string Name { get; set; } public string Value { get; set; } }.

The Serializable attribute is not needed for XML serialization – Thomas Levesque May 18 '10 at 18:35 Yes.. exactly.. thanks, worked just like I needed it, what a pain lol, knew it had to be a tag just didn't see which one I needed to use – LeeHull May 18 '10 at 18:36.

Use the XmlElement attribute on the Details property : public class User { public string UserName {get;set;} public string Email {get;set;} XmlElement public Details Details {get;set;} } If you don't, the XmlSerializer assumes that your elements are wrapped in a parent element.

Use Linq To XML.. something like this XElement xe = XDocument. Load("PATH_HERE"). Root; var v = from k in xe.Descendants() select new { id = k.

Element("id"), data = k. Element("data") }.

Here's a sample program which makes no changes to the XML but deserializes the Details node properly: using System; using System. Text; using System. Xml; using System.Xml.

Linq; using System.Xml. Serialization; using System. IO; using System.

Diagnostics; using System.Collections. Generic; namespace ConsoleApplication1 { System.Xml.Serialization. XmlRootAttribute(Namespace = "", IsNullable = false) public class DataSet { public User User { get; set; } } public class User { public string UserName { get; set; } public string Email { get; set; } System.Xml.Serialization.

XmlElementAttribute("Details") public Details Details { get; set; } } public class Details { public int ID { get; set; } public string Name { get; set; } public string Value { get; set; } } class Program { static void Main(string args) { string xmlFragment = @" Test test@test. Com 1 TestDetails 1 2 Testing 3 "; StringReader reader = new StringReader(xmlFragment); XmlSerializer xs = new XmlSerializer(typeof(DataSet)); DataSet ds = xs. Deserialize(reader) as DataSet; User user = ds.

User; Console. WriteLine("User name: {0}", user. UserName); Console.

WriteLine("Email: {0}", user. Email); foreach (Details detail in user. Details) { Console.

WriteLine("Detail ID: {0}", detail. ID); Console. WriteLine("Detail Name: {0}", detail.Name); Console.

WriteLine("Detail Value: {0}", detail. Value); } // pause program execution to review results... Console. WriteLine("Press enter to exit"); Console.ReadLine(); } } }.

You need to do something like: using System. IO; using System.Xml. Serialization; namespace TestSerialization { class Program { static void Main(string args) { string content= @" Test [email protected] 1 TestDetails 1 2 Testing 3 "; XmlSerializer serializaer = new XmlSerializer(typeof(DataSet)); DataSet o = (DataSet) serializaer.

Deserialize(new StringReader(content)); } } public class User { public string UserName { get; set; } public string Email { get; set; } public Detail Details { get; set; } } public class Detail { public int ID { get; set; } public string Name { get; set; } public string Value { get; set; } } public class DataSet { public User User; } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions