Solved

Create XML in C#

Posted on 2008-10-23
6
1,341 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
Comment Utility
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
Comment Utility
good example on how to build document

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

Expert Comment

by:silemone
Comment Utility
this is the other option that you wanted...use the framework and build your xml like that...
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 17

Expert Comment

by:xDJR1875
Comment Utility
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:xDJR1875
Comment Utility
that would be using System.Xml
0
 
LVL 23

Accepted Solution

by:
adathelad earned 500 total points
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

8 Experts available now in Live!

Get 1:1 Help Now