Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Create XML Document with multiple tier levels with C#

Posted on 2012-08-21
2
Medium Priority
?
642 Views
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"?>
<Candidates>
   <Candidate>
      <CandidateRecordInfo>
         <Id>
            <IdValue name="source_id">129301</IdValue>
            <IdValue name="pers_id">123769B9</IdValue>
         </Id>
      </CandidateRecordInfo>
      <CandidateProfile>
         <PersonalData>
            <PersonName>
               <GivenName>Jon</GivenName>
               <PreferredGivenName></PreferredGivenName>
               <MiddleName />
               <FamilyName>Doe</FamilyName>
               <Affix type="generation" />
               <Affix type="qualification" />
            </PersonName>
         </PersonalData>
      </CandidateProfile>
   </Candidate>
</Candidates>

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);
            doc.AppendChild(docNode);

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

            XmlNode nameNode = doc.CreateElement("IdValue");
            XmlAttribute productAttribute = doc.CreateAttribute("name");
            productAttribute.Value = "source_id";
            nameNode.Attributes.Append(productAttribute);
            nameNode.AppendChild(doc.CreateTextNode("129301"));
            productNode.AppendChild(nameNode);
            string xmlOutput = doc.OuterXml;
            doc.Save("c:\\temp\\output.xml");
        }

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

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

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

What am I missing?
0
Comment
Question by:dbeayon
2 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 38318047
Hi dbeayon;

Uncomment the two lines of code and modify this line of code
productsNode.AppendChild(productNode);

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

0
 

Author Closing Comment

by:dbeayon
ID: 38318188
Perfect
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

564 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