intikhabk
asked on
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>
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>
XDocument works well...better approach? if you're using d database then return data from dataBase as xml
this is the other option that you wanted...use the framework and build your xml like that...
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(oXMLDo c.CreatePr ocessingIn struction( "xml", "version=\"1.0\" encoding=\"utf-8\""));
oRootNode = oXMLDoc.CreateElement("Dat a");
oNode1 = oXMLDoc.CreateElement("Boo ks");
oRootNode.AppendChild(oXML Node);
oNode2 = oXMLDoc.CreateElement("Boo k");
oXMLAttribute = oXMLDoc.CreateAttribute("k ey");
oXMLAttribute.Value = "key1";
oNode2.Attributes.Append(o XMLAttribu te);
oXMLAttribute = oXMLDoc.CreateAttribute("v alue");
oXMLAttribute.Value = "Value1";
oNode2.Attributes.Append(o XMLAttribu te);
oNode1.AppendChild(oNode2) ;
oXMLDoc.Save("filename.xml ");
XmlDataDocument oXMLDoc = new XmlDataDocument();
XmlNode oRootNode;
XmlNode oXMLNode;
XmlNode oNode1;
XmlNode oNode2;
XmlAttribute oXMLAttribute;
oXMLDoc.AppendChild(oXMLDo
oRootNode = oXMLDoc.CreateElement("Dat
oNode1 = oXMLDoc.CreateElement("Boo
oRootNode.AppendChild(oXML
oNode2 = oXMLDoc.CreateElement("Boo
oXMLAttribute = oXMLDoc.CreateAttribute("k
oXMLAttribute.Value = "key1";
oNode2.Attributes.Append(o
oXMLAttribute = oXMLDoc.CreateAttribute("v
oXMLAttribute.Value = "Value1";
oNode2.Attributes.Append(o
oNode1.AppendChild(oNode2)
oXMLDoc.Save("filename.xml
that would be using System.Xml
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.