Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1352
  • Last Modified:

Create XML in C#

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
intikhabk
Asked:
intikhabk
  • 3
  • 2
1 Solution
 
silemoneCommented:
XDocument works well...better approach?  if you're using d database then return data from dataBase as xml
0
 
silemoneCommented:
good example on how to build document

http://www.example-code.com/csharp/csCreateXml.asp
0
 
silemoneCommented:
this is the other option that you wanted...use the framework and build your xml like that...
0
Independent Software Vendors: 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!

 
Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
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
 
Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
that would be using System.Xml
0
 
adatheladCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now