Solved

Create XML in C#

Posted on 2008-10-23
6
1,345 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

735 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