Solved

Create XML in C#

Posted on 2008-10-23
6
1,348 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

615 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