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

Posted on 2009-04-29
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
    LVL 26

    Expert Comment

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


    Author Comment

    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

    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

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API ( has made its way into the popular lexicon of the English language.  A few years ago, …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    730 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now