convert database to xml file (but i want column as attribute) [modified]
-
hi.. i already convert database table to xml but it create new element for each column but i want each column as attribute.. code i have is as below: public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { DataSet ds = GetDataSet(); myXMLControl.DocumentContent = ds.GetXml(); } public DataSet GetDataSet() { DataSet result = new DataSet(); string connString = "server=192.0.0.1;user id=test;password=123;persist security info=True;database=MLM1"; using (MySqlConnection myConnection = new MySqlConnection(connString)) { string query = "Select userid,code,parent_id from _relation"; MySqlCommand myCommand = new MySqlCommand(query, myConnection); MySqlDataAdapter myAdapter = new MySqlDataAdapter(); myCommand.CommandType = CommandType.Text; myAdapter.SelectCommand = myCommand; myAdapter.Fill(result); myAdapter.Dispose(); } result.DataSetName = "root"; result.Tables[0].TableName = "Node"; DataRelation relation = new DataRelation("ParentChild",//tab.Columns.Add("userid"),tab.Columns.Add("parent_id"),true);
-
hi.. i already convert database table to xml but it create new element for each column but i want each column as attribute.. code i have is as below: public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { DataSet ds = GetDataSet(); myXMLControl.DocumentContent = ds.GetXml(); } public DataSet GetDataSet() { DataSet result = new DataSet(); string connString = "server=192.0.0.1;user id=test;password=123;persist security info=True;database=MLM1"; using (MySqlConnection myConnection = new MySqlConnection(connString)) { string query = "Select userid,code,parent_id from _relation"; MySqlCommand myCommand = new MySqlCommand(query, myConnection); MySqlDataAdapter myAdapter = new MySqlDataAdapter(); myCommand.CommandType = CommandType.Text; myAdapter.SelectCommand = myCommand; myAdapter.Fill(result); myAdapter.Dispose(); } result.DataSetName = "root"; result.Tables[0].TableName = "Node"; DataRelation relation = new DataRelation("ParentChild",//tab.Columns.Add("userid"),tab.Columns.Add("parent_id"),true);
DataSet
provides limited options to extend the XML writing behavior. To create a custom XML format like you mentioned, you may need to create your own serializable type and add proper attributes to it. You can use XmlSerializer[^] to serialize this into a stream or file.Navaneeth How to use google | Ask smart questions
-
DataSet
provides limited options to extend the XML writing behavior. To create a custom XML format like you mentioned, you may need to create your own serializable type and add proper attributes to it. You can use XmlSerializer[^] to serialize this into a stream or file.Navaneeth How to use google | Ask smart questions
i tried that but i hav no gud idea for serializable.. can you post code snippet for that?? thanks..
-
i tried that but i hav no gud idea for serializable.. can you post code snippet for that?? thanks..
punit_belani wrote:
i hav no gud idea for serializable
What is wrong in checking MSDN documentation? :doh: Any way here is a sample,
using System.Xml;
using System.Xml.Serialization;
using System.IO;[Serializable]
public class Person
{
[XmlAttribute("Name")]
public string Name { get; set; }\[XmlElement\] public int Age { get; set; }
}
Person person = new Person()
{
Name = "Chuck norris",
Age = 40
};using (FileStream fs = new FileStream("person.xml", FileMode.Create, FileAccess.Write))
{
XmlSerializer serializer = new XmlSerializer(typeof(Person));
serializer.Serialize(fs, person);
}The attributes on each property tells the
XmlSerializer
object about the way it should produce output XML. :)Navaneeth How to use google | Ask smart questions