How can i serialize two objects, of Class A and Class B, to xml so that it can be of the following format:
<root>
<objClassA> //Parent Node
<objClassB> // child node of objClassA
</objClassB>
</objClassA>
</root>
[Serializable(true)]
public class objClassB
{
}
[Serializable(true)]
public class objClassA
{
public objClassB instance;
}
Then use the XmlSerializer for an instance of objClassA and it will automatically place the instance of the b inside itself as a child.
using System;
using System.IO;
using System.Xml.Serialization;
void Write(root rootInstance)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(root));
using (FileStream fileStream = new FileStream("filepath.xml", FileMode.Create))
{
xmlSerializer.Serialize(fileStream, rootInstance);
}
}
public class root
{
public ClassA objClassA { get; set; }
}
public class ClassA
{
public ClassB objClassB { get; set; }
}
public class ClassB { }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With