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 { }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.