Click here to Skip to main content
15,881,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Experts,

Can anybody suggest me a best approach on how to generate a XML file programmatically using C# other than XmlReader/XmlWriter? (so that parsing it should be easier).

Basically, it is a Xml Hbm mapping file for NHibernate. Any .NET Api available for reading/writing Hbm files like for Xaml we have XamlReader/XamlWriter.

Thanks.
Posted
Comments
George Jonsson 18-Dec-14 3:24am    
Are you limited to use .NET3.0 or can you use .NET3.5?
Mohammed Hameed 18-Dec-14 4:29am    
Using .Net 4.0

You can use XmlDocument[^].
C#
XmlDocument xmlDoc = new XmlDocument();
XmlElement elRoot = xmlDoc.CreateElement("body");
xmlDoc.AppendChild(elRoot);

XmlElement elParam = xmlDoc.CreateElement("param");
XmlText elValue = xmlDoc.CreateTextNode("MyClass_SEQ");
elParam.AppendChild(elValue);
elParam.SetAttribute("name", "sequence");
elRoot.AppendChild(elParam);

XmlElement elProp = xmlDoc.CreateElement("property");
elProp.SetAttribute("name", "Prop1");
elProp.SetAttribute("column", "Column1");
elProp.SetAttribute("type", "Int32");
elRoot.AppendChild(elProp);

string s = xmlDoc.OuterXml;


If you can use .NET3.5 or later you can also use XDocument[^]
C#
XDocument doc = new XDocument();
XElement xeBody = new XElement("body");     // Root element
XElement xeParam = new XElement("param", "MyClass_SEQ");
xeParam.Add(new XAttribute("name", "sequence"));
xeBody.Add(xeParam);

XElement xeProperty = new XElement("property");
xeProperty.Add(new XAttribute("name", "Prop1"));
xeProperty.Add(new XAttribute("column", "Column1"));
xeProperty.Add(new XAttribute("type", "Int32"));
xeBody.Add(xeProperty);

doc.Add(xeBody);
doc.Save(@"c:\Temp\test.xml");
 
Share this answer
 
Comments
Maciej Los 18-Dec-14 8:52am    
+5!
George Jonsson 18-Dec-14 22:50pm    
Thanks Maciej.
Always nice when an answer is appreciated.
System.Data.DataSet ds = new System.Data.DataSet();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("Select * from demo", "Data Source=184.107.55.15;Initial Catalog=demo;User ID=sa;Password=1947");
da.Fill(ds);
// Write to XML here

ds.WriteXml("D:\\output.xml", System.Data.XmlWriteMode.WriteSchema);
 
Share this answer
 
v3
Comments
Mohammed Hameed 1-Jun-13 6:05am    
I dont want to use a DataSet. I want to create HBM file tags like:


<hibernate-mapping assembly="MyAssembly" namespace="MyNamespace" xmlns="urn:nhibernate-mapping-2.2">
<class name="MyClass" table="MyTable" lazy="false">
<id name="PrimaryPropertyId" column="PrimaryColumn_ID" type="Int32">
<generator class="native">
<param name="sequence">MyClass_SEQ</param>


<property name="Prop1" column="Column1" type="Int32"/>
<property name="Prop2" column="Column2" type="Int32"/>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900