Reputation: 53
I have the following XML:
<Vehicle>
<Type>
<ISN>213123214</ISN>
<Name>ddsd</Name>
</Type>
<RegNo>1234</RegNo>
<Mark>
<ISN>423434234</ISN>
<Name>asdasd</Name>
</Mark>
<Model>
<ISN>434234324324</ISN>
<Name>asddsa</Name>
</Model>
<EstimatedPrice>
<Amount>15000</Amount>
<AmountPrev />
<Currency>
<Code>R</Code>
<Name>RU</Name>
</Currency>
</EstimatedPrice>
</Vehicle>
Well, I am trying to deserialize this to the destination object using C#. This is my target class:
public class Vehicle {
[XmlElement("Type")]
public Type Type { get; set; }
[XmlElement("Mark")]
public Mark Brand { get; set; }
[XmlElement("Model")]
public Mark Brand { get; set; }
[XmlElement("EstimatedPrice")]
public Estimation Estimation { get; set; }
}
The question is how to deserialize attributes Mark, Model and Type correctly? Now I'm getting empty objects instead of data. I've tried to specify "Vehicle" as the root tag for this subclasses, but it doesn't had an effect.
P.S. Classes Mark, Model and Type are derived from the base class:
[Serializable]
public class ResponseItem
{
[XmlElement("ISN")]
string ISN { get; set; }
[XmlElement("Name")]
string Name { get; set; }
}
For deserializing I'm using XmlSerializer class. All the tags with to subtags or with subtags with unique names are deserialized correctly.
What have I missed?
Thanks.
Upvotes: 1
Views: 1275
Reputation: 34421
I like working backwards and create a test serializer to validate my structures.
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)
{
Vehicle vehicle = new Vehicle() {
Type = new Type()
{
ISN = "213123214",
Name = "ddsd"
},
RegNo = 1234,
Brand = new Mark()
{
ISN = "423434234",
Name = "asdasd"
},
Model = new Mark()
{
ISN = "434234324324",
Name = "asddsa"
},
estimation = new Estimation()
{
Amount = 15000,
AmountPrev = null,
currency = new Currency()
{
Code = "R",
Name = "RU"
}
}
};
XmlSerializer serializer = new XmlSerializer(typeof(Vehicle));
StreamWriter writer = new StreamWriter(FILENAME);
serializer.Serialize(writer, vehicle);
writer.Flush();
writer.Close();
writer.Dispose();
}
}
[XmlRoot("Vehicle")]
public class Vehicle
{
[XmlElement("Type")]
public Type Type { get; set; }
[XmlElement("Mark")]
public Mark Brand { get; set; }
[XmlElement("Model")]
public Mark Model { get; set; }
[XmlElement("RegNo")]
public int RegNo { get; set; }
[XmlElement("EstimatedPrice")]
public Estimation estimation { get; set; }
}
[XmlRoot("EstimationPrice")]
public class Estimation
{
[XmlElement("Amount")]
public int Amount { get; set; }
[XmlElement("AmountPrev")]
public int? AmountPrev { get; set; }
[XmlElement("Currency")]
public Currency currency { get; set; }
}
[XmlRoot("Currency")]
public class Currency
{
[XmlElement("Code")]
public string Code { get; set; }
[XmlElement("Name")]
public string Name { get; set; }
}
[XmlRoot("Type")]
public class Type : ResponseItem
{
}
[XmlRoot("Mark")]
public class Mark : ResponseItem
{
}
[XmlRoot("ResponseItem")]
public class ResponseItem
{
[XmlElement("ISN")]
public string ISN { get; set; }
[XmlElement("Name")]
public string Name { get; set; }
}
}
Upvotes: 0
Reputation: 27962
Properties in your ResponseItem
base class must be public
public class ResponseItem
{
[XmlElement("ISN")]
public string ISN { get; set; }
[XmlElement("Name")]
public string Name { get; set; }
}
XML serializer won't process non-public properties
Upvotes: 1