Create XML Document with multiple tier levels with C#

Posted on 2012-08-21
Last Modified: 2012-08-21
I am trying to create the following XML Document in VS2012.  I can create the first

<?xml version="1.0" encoding="UTF-8"?>
            <IdValue name="source_id">129301</IdValue>
            <IdValue name="pers_id">123769B9</IdValue>
               <MiddleName />
               <Affix type="generation" />
               <Affix type="qualification" />

Here is the code that I have assembled

         private static void createXMLDoc()
            XmlDocument doc = new XmlDocument();
            XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);

            XmlNode productsNode = doc.CreateElement("Candidates");
            //XmlNode candidateNode = doc.CreateElement("Candidate");
            XmlNode productNode = doc.CreateElement("CandidateRecordInfo");

            XmlNode nameNode = doc.CreateElement("IdValue");
            XmlAttribute productAttribute = doc.CreateAttribute("name");
            productAttribute.Value = "source_id";
            string xmlOutput = doc.OuterXml;

The code will generate the following XML
<?xml version="1.0" encoding="UTF-8"?>
    <IdValue name="source_id">129301</IdValue>

The issue that I am having is getting the commented lines to work.

            //XmlNode candidateNode = doc.CreateElement("Candidate");

What am I missing?
Question by:dbeayon
    LVL 62

    Accepted Solution

    Hi dbeayon;

    Uncomment the two lines of code and modify this line of code

    Open in new window

    To this
    // You need to assign the productNode as a chile of candidateNode and NOT productsNode
    candidateNode.AppendChild( productNode );

    Open in new window


    Author Closing Comment


    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    754 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

    19 Experts available now in Live!

    Get 1:1 Help Now