I am deserializing a given XML file into a single object, which I have working fine. However, I now which to adapt the XML file so that I extra attributes, which should be deserialized into a separate object, so from the one given xml file i can populate two objects, is this possible within one operation?
Here is my current code and XML:
XML As it is now:
<NameCollection>
<Names>
<Name>
<description></description>
<Name>
<Name>
<description></description>
<Name>
</Names>
</NameCollection>
XML As I wish it to be:
<NameCollection>
<GenericName></GenericName>
<GenericDescription></GenericDescription>
<Names>
<Name>
<description></description>
<Name>
<Name>
<description></description>
<Name>
</Names>
</NameCollection>
Code:
NameCollection commands;
XMLSerializer serializer = new XMLSerializer(typeof(NameCollection));
StreamReader streamReader = new StreamReader(xmlpath);
commands = (NameCollection)serializer.Derserialize(streamreader);
streamReader.Close();
And the current object:
[Serializable()]
public class TestCommand
{
public string description{get;set;}
}
[Serializable()]
[XmlRoot("NameCollection")]
public class NameCollection
{
[XmlArray("Commands")]
[XmlArrayItem("Command", typeof(TestCommand))]
public TestCommand[] TestCommand {get;set;}
}
I then wish to add GenericName and GenericDescription attributes to another separate object, this is what im stuck on.
I think what your after is to have your class reflect the structure of the XML, rather than two classes. So you want a structure like:
then serialize it in exactly the same way, job done.