Deserializing Xml to Objects in C#

How to deserialize xml to object

Your classes should look like this

[XmlRoot("StepList")]
public class StepList
{
[XmlElement("Step")]
public List<Step> Steps { get; set; }
}

public class Step
{
[XmlElement("Name")]
public string Name { get; set; }
[XmlElement("Desc")]
public string Desc { get; set; }
}

Here is my testcode.

string testData = @"<StepList>
<Step>
<Name>Name1</Name>
<Desc>Desc1</Desc>
</Step>
<Step>
<Name>Name2</Name>
<Desc>Desc2</Desc>
</Step>
</StepList>";

XmlSerializer serializer = new XmlSerializer(typeof(StepList));
using (TextReader reader = new StringReader(testData))
{
StepList result = (StepList) serializer.Deserialize(reader);
}

If you want to read a text file you should load the file into a FileStream
and deserialize this.

using (FileStream fileStream = new FileStream("<PathToYourFile>", FileMode.Open)) 
{
StepList result = (StepList) serializer.Deserialize(fileStream);
}

Deserializing XML to objects which has list of objects in C#

Variable need to be public.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
string xml = File.ReadAllText(FILENAME);

XmlSerializer serializer = new XmlSerializer(typeof(Result));
StringReader rdr = new StringReader(xml);
Result resultingMessage = (Result)serializer.Deserialize(rdr);

}
}
public enum ReportType
{
[XmlEnum("0")]
InternalErrorReport,
[XmlEnum("1")]
ErrorReport,
[XmlEnum("2")]
InternalSuccessReport
}

[XmlRoot(ElementName = "result")]
public class Result
{
[XmlElement(ElementName = "reporttype")]
public ReportType reportType { get; set; }
public Items items { get; set; }
public string error { get; set; }

}
[XmlRoot("items")]
public class Items
{
[XmlElement(ElementName = "item")]
public List<Item> items = new List<Item>();
}
[XmlRoot("item")]
public class Item
{
[XmlElement(ElementName = "sku")]
public string sku { get; set; }
[XmlElement(ElementName = "style")]
public string style { get; set; }
[XmlElement(ElementName = "reason")]
public string reason { get; set; }
}
}

Deserialize XML into object with dynamic child elements

I've managed to resolve this using a dynamic type when deserializing.
When I deserialize ValuesRead, it is a defined as a dynamic type.

When deserialized, it turns into an XmlNode and from there I iterate over the node use the Name and InnerText values to read all the data.

Deserializing XML to Objects in C#

Create a class for each element that has a property for each element and a List or Array of objects (use the created one) for each child element. Then call System.Xml.Serialization.XmlSerializer.Deserialize on the string and cast the result as your object. Use the System.Xml.Serialization attributes to make adjustments, like to map the element to your ToDoList class, use the XmlElement("todo-list") attribute.

A shourtcut is to load your XML into Visual Studio, click the "Infer Schema" button and run "xsd.exe /c schema.xsd" to generate the classes. xsd.exe is in the tools folder. Then go through the generated code and make adjustments, such as changing shorts to ints where appropriate.



Related Topics



Leave a reply



Submit