Deserializing XML to C# Object returning null values

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<p style="margin:0cm 0cm 10pt <span style="font-family:Calibri;font-size:small Hi I am trying to deserialize an XML file to a C# object. The problem is that the properties of my object are returned as null. I think it must be something to do with my XML file. Has anyone got any ideas?
<p style="margin:0cm 0cm 10pt <span style="font-family:Calibri;font-size:small http://www.yourcodefactory.com/forum Thanks
<p style="margin:0cm 0cm 10pt <span style="font-family:Calibri;font-size:small XML File:<br/><span style="font-family:Calibri;font-size:small
<pre lang=x-xml><?xml version="1.0" encoding="utf-8"?>
<speed>
<maxspeed>50</maxspeed>
<highjogspeed>10</highjogspeed>
<mediumjogspeed>10</mediumjogspeed>
<slowjogspeed>10</slowjogspeed>
<mincornerspeed>10</mincornerspeed>
<fasthomespeed>20</fasthomespeed>
<slowhomespeed>10</slowhomespeed>
<creepspeed>25</creepspeed>
</speed>
[/code]
<p style="margin:0cm 0cm 10pt
<p style="margin:0cm 0cm 10pt <span style="font-family:Calibri;font-size:small <br/><span style="font-family:Calibri;font-size:small C# Code:<br/><br/>
<pre lang="x-c# [XmlRoot(Namespace = "urn:speed")]
public class OrderedItem
{
public string maxspeed;
public string highjogspeed;
public string mediumjogspeed;
public string slowjogspeed;
public string mincornerspeed;
public string fasthomespeed;
public string slowhomespeed;
public string creepspeed;
}

public class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.DeserializeObject(@"C:test.xml");
}

private void DeserializeObject(string filename)
{
XmlRootAttribute rootAttribute = new XmlRootAttribute("speed");
XmlSerializer deserializer = new XmlSerializer((typeof(OrderedItem)), rootAttribute);

TextReader tr = new StreamReader(filename);
XmlTextReader reader = new XmlTextReader(tr);
OrderedItem i = (OrderedItem)deserializer.Deserialize(reader);

Console.Write(
i.maxspeed + "t" +
i.highjogspeed + "t" +
i.mediumjogspeed + "t" +
i.slowjogspeed + "t" +
i.mincornerspeed + "t" +
i.fasthomespeed + "t" +
i.slowhomespeed + "t" +
i.creepspeed + "t");

Console.ReadLine();
}
}[/code]

View the full article
 
Back
Top