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

Posted on 2009-04-29
Medium Priority
Last Modified: 2012-06-21
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.

Question by:meetpd
  • 2
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24264085
The way you have explained your structure of classes and properties in it following xml structure will be generated on serialization


Author Comment

ID: 24266311
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?
LVL 26

Accepted Solution

Anurag Thakur earned 1500 total points
ID: 24266847
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


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question