I have the following XML file that i am trying to read into a class in c# using DE-serialization:
You don't need to explicitly declare the properties. Just make sure the names match. The only non-default portion is the [XmlArrayItem("Map")]
attribute you will need to use the different name for the map array.
You can, however, use differing names as I have for the COM_Property
and WEB_Property
by specifying the XmlElementAttribute
.
class Program
{
static void Main(string[] args)
{
string testData = @"
InfoRequest
CInfoReq
";
var sr = new System.IO.StringReader(testData);
var xs = new XmlSerializer(typeof(PropertiesMapping));
object result = xs.Deserialize(sr);
}
}
[Serializable]
public class PropertiesMapping
{
public Property Property { get; set; }
}
[Serializable]
public class Property
{
[XmlElement("WEB_Class")]
public string WebClass { get; set; }
[XmlElement("COM_Class")]
public string ComClass { get; set; }
[XmlArrayItem("Map")]
public Mapping[] Mappings { get; set; }
}
[Serializable]
public class Mapping
{
[XmlElement("WEB_Property")]
public string WebProperty { get; set; }
[XmlElement("COM_Property")]
public string ComProperty { get; set; }
}