OK I'm using ASP MVC and am trying to load some XML data which I need to serialize as I'm converting it to JSON.
What I'm failing to understand is, my XML has more than one descendant. So, when I serialize it, in the model I need to store the data from each descendant in its own list right?
I've done that, so I've got two seperate lists. So, how can I serialize this all into the one XML doc?
This is an example of my XML markup:
<vehicle>
<make>Suzuki</make>
<color>Yellow></color>
<owner>
<id>0</id>
<name>Joe Bloggs</name>
</owner>
<owner>
<id>1</id>
<name>Foo Bar</name>
</owner>
</vehicle>
Here's my XML model:
public partial class vehicle
{
private string makeField;
private string colorField;
private vehicleOwner[] ownerField;
/// <remarks/>
public string make {get; set;}
/// <remarks/>
public string color{get; set;}
[System.Xml.Serialization.XmlElementAttribute("owner")]
public vehicleOwner[] owner{ get; set;}
// Keep this in a list to serialize it
List<vehicle> vehicleList = new List<vehicle>();
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class vehicleOwner
{
private int idField;
private string nameField;
/// <remarks/>
public int id { get; set;}
/// <remarks/>
public string nameField {get; set;}
// Stick it in a list so I can serialize it later
List<vehicleOwner> vehicleOwnerList = new List<vehicleOwner>();
}
}
So, now I have my two lists containing XML data: vehicleOwnerList and vehicleList. But these are all the one XML document. So how can I serialize this for both lists? Would I have to merge them into one list, and if so how would I do that?
So I.E.
List<vehicleOwner> listCombinedActions = new List<vehicleOwner>();
XmlDocument xmlDoc = new XmlDocument();
XPathNavigator nav = xmlDoc.CreateNavigator();
using (XmlWriter writer = nav.AppendChild())
{
XmlSerializer ser = new XmlSerializer(typeof(List<vehicle>), new XmlRootAttribute("owner"));
ser.Serialize(writer, listCombinedActions);
}
Cheers
No comments:
Post a Comment