Reputation: 822
I've seen an example that uses a wrapper class to do this, but i was wondering if there is a better (simpler) way.
My classes are as follows:
public class PartData
{
public List<PartInfo> PartList { get; set; }
}
public class PartInfo
{
public string PartNumber { get; set; }
public string OEMNumbers { get; set; }
public List<VehicleApplication> VehicleApplications { get; set; }
}
public class VehicleApplication
{
public string Year { get; set; }
public string Make { get; set; }
public string Model { get; set; }
}
Serialize code:
//serialize
private void serialize()
{
PartData p = GetParts();
SerializeClass(p);
}
public void SerializeClass(object instance)
{
var serializer = new XmlSerializer(typeof(PartData));
using (var writer = new StreamWriter("C:\\TestFile.xml"))
{
serializer.Serialize(writer, instance);
}
}
when serialized i get the following output:
<PartData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<PartList>
<PartInfo>
<PartNumber>12345</PartNumber>
<OEMNumbers>14556, 14557, 14558, 14559</OEMNumbers>
<VehicleApplications>
<VehicleApplication>
<Year>2001</Year>
<Make>Ford</Make>
<Model>F150</Model>
</VehicleApplication>
<VehicleApplication>
<Year>2001</Year>
<Make>Ford</Make>
<Model>F150</Model>
</VehicleApplication>
<VehicleApplication>
<Year>2001</Year>
<Make>Ford</Make>
<Model>F150</Model>
</VehicleApplication>
<VehicleApplication>
<Year>2001</Year>
<Make>Ford</Make>
<Model>F150</Model>
</VehicleApplication>
</VehicleApplications>
</PartInfo>
</PartList>
</PartData>
Ideally i would like to make PartNumber an attribute like so:
<PartData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<PartList>
<PartInfo>
<PartNumber Id ="12345">
<OEMNumbers>14556, 14557, 14558, 14559</OEMNumbers>
<VehicleApplications>
<VehicleApplication>
<Year>2001</Year>
<Make>Ford</Make>
<Model>F150</Model>
</VehicleApplication>
<VehicleApplication>
<Year>2001</Year>
<Make>Ford</Make>
<Model>F150</Model>
</VehicleApplication>
<VehicleApplication>
<Year>2001</Year>
<Make>Ford</Make>
<Model>F150</Model>
</VehicleApplication>
<VehicleApplication>
<Year>2001</Year>
<Make>Ford</Make>
<Model>F150</Model>
</VehicleApplication>
</VehicleApplications>
</Partnumber>
</PartInfo>
</PartList>
</PartData>
I've tried adding:
public class PartInfo
{
[XmlAttribute("Id")]
public string PartNumber { get; set; }
public string OEMNumbers { get; set; }
public List<VehicleApplication> VehicleApplications { get; set; }
}
but creates:
<PartInfo Id="12345">
when i need:
<PartNumber Id="12345">
Upvotes: 1
Views: 900
Reputation: 43300
I think this is the only way to achieve what you are trying to achieve
public class PartDetail
{
[XmlAttribute]
public string ID{ get; set; }
public string OEMNumbers { get; set; }
public List<VehicleApplication> VehicleApplications { get; set; }
}
public class PartInfo
{
public PartDetail { get; set; }
}
In answer to your first comment, this is the easiest way to achieve it, the alternative is to write a custom serializer
Upvotes: 2