Problem creating Subnode while creating XML file through XML Serialization using C#

I am creating an XML file using XML Serialization by converting XML schema to class file.

In this class file, I have following elements:

Class is 'PersonalInfo'
Class inside 'PersonalInfo' is 'AssesseeName'
Attributes of 'AssesseeName' are 'First Name', 'Last Name'.

I want to know the approach about how to have the above node structure when we create XML file through XML Serialization.

Who is Participating?
Anurag ThakurTechnical ManagerCommented:
from the following site
erialize (convert an object instance to an XML document):
// Assuming obj is an instance of an object
XmlSerializer ser = new XmlSerializer(obj.GetType());
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter writer = new System.IO.StringWriter(sb);
ser.Serialize(writer, obj);
XmlDocument doc = new XmlDocument();
Deserialize (convert an XML document into an object instance):
//Assuming doc is an XML document containing a serialized object and objType is a System.Type set to the type of the object.
XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);
XmlSerializer ser = new XmlSerializer(objType);
object obj = ser.Deserialize(reader);
// Then you just need to cast obj into whatever type it is eg:
MyClass myObj = (MyClass)obj;

Open in new window

Anurag ThakurTechnical ManagerCommented:
The way you have explained your structure of classes and properties in it following xml structure will be generated on serialization

meetpdAuthor Commented:
Yes what you have understood is correct. What I am not able to understand is how do I achieve that through C#. Can you please suggest?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.