Reputation: 1844
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>
Upvotes: 3
Views: 283
Reputation: 5377
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 { }
Upvotes: 1
Reputation: 22956
[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.
Upvotes: 1