Solved

Create XML in C#

Posted on 2008-10-23
6
1,343 Views
Last Modified: 2012-05-05
How do I create the below XML example using XDocument class in C#

Is there any better approach other than XDocument

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Data>
  <Books>
    <Book key="key1" value="value1" />
    <Book key="key2" value="value2" />
  </Books>
  <CDS>
    <CD>
      <Key>key1</Key>
      <Value>value1</Value>
    </CD>
    <CD>
      <Key>key2</Key>
      <Value>value2</Value>
    </CD>
  </CDS>
</Data>

0
Comment
Question by:intikhabk
  • 3
  • 2
6 Comments
 
LVL 21

Expert Comment

by:silemone
ID: 22790031
XDocument works well...better approach?  if you're using d database then return data from dataBase as xml
0
 
LVL 21

Expert Comment

by:silemone
ID: 22790051
good example on how to build document

http://www.example-code.com/csharp/csCreateXml.asp
0
 
LVL 21

Expert Comment

by:silemone
ID: 22790064
this is the other option that you wanted...use the framework and build your xml like that...
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 17

Expert Comment

by:Daniel Reynolds
ID: 22790148
The following bit of code might help if you are trying to do it all programmatically. It will give you an idea of how to traverse and build it from the ground up.

            XmlDataDocument oXMLDoc = new XmlDataDocument();
            XmlNode oRootNode;
            XmlNode oXMLNode;
             XmlNode oNode1;
            XmlNode oNode2;
            XmlAttribute oXMLAttribute;

               oXMLDoc.AppendChild(oXMLDoc.CreateProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\""));
                oRootNode = oXMLDoc.CreateElement("Data");
                oNode1 = oXMLDoc.CreateElement("Books");
                oRootNode.AppendChild(oXMLNode);
                oNode2 = oXMLDoc.CreateElement("Book");
                oXMLAttribute = oXMLDoc.CreateAttribute("key");
                oXMLAttribute.Value = "key1";
                oNode2.Attributes.Append(oXMLAttribute);
                oXMLAttribute = oXMLDoc.CreateAttribute("value");
                oXMLAttribute.Value = "Value1";
                oNode2.Attributes.Append(oXMLAttribute);

                oNode1.AppendChild(oNode2);

                oXMLDoc.Save("filename.xml");
0
 
LVL 17

Expert Comment

by:Daniel Reynolds
ID: 22790157
that would be using System.Xml
0
 
LVL 23

Accepted Solution

by:
adathelad earned 500 total points
ID: 22790202
Hi,

I'd recommend using the XmlTextWriter class instead, you'll get better performance/less memory intensive than loading it into a DOM structure.

http://msdn.microsoft.com/en-us/library/system.xml.xmltextwriter.aspx
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2870256&SiteID=1

Recently researched into the various approaches to write XML, and if you don't immediately want to manipulate the resulting xml document once you've created it, XmlTextWriter should be the preferred way.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

16 Experts available now in Live!

Get 1:1 Help Now