Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET2.0 C#2.0 .NET3.0 C#3.0 VS2008 , +
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 1-Jun-13 0:44am
Comments
George Jonsson at 18-Dec-14 3:24am
   
Are you limited to use .NET3.0 or can you use .NET3.5?
Mohammed Hameed at 18-Dec-14 4:29am
   
Using .Net 4.0
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You can use XmlDocument[^].
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[^]
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");
  Permalink  
Comments
Maciej Los at 18-Dec-14 8:52am
   
+5!
George Jonsson at 18-Dec-14 22:50pm
   
Thanks Maciej.
Always nice when an answer is appreciated.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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);
  Permalink  
v3
Comments
Mohammed Hameed at 1-Jun-13 6:05am
   
I dont want to use a DataSet. I want to create HBM file tags like:
 





<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)



Advertise | Privacy | Mobile
Web04 | 2.8.150302.1 | Last Updated 18 Dec 2014
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100