C# .NET - Create a XML file - Asked By Srinivasan S on 16-Nov-11 10:03 PM

Hi,

Good Morning to all!

I want to save the data in the format of XML file using c# windows application.

Suppose, giving Name and Section in the textboxes and click save button,it should create and save as XML file.

After I close and open the same form,the XML file data should be in my form datatable.(Created a datatable already with columns like name and section,XML data should be in the datatable.)

How can i do this?

Help me.....
Web Star replied to Srinivasan S on 16-Nov-11 10:39 PM
you can use XmlTextWriter (Namespace: System.Xml) to write xml file in code. This is good article to describe all in details step by step
http://www.codeproject.com/KB/cpp/XMLReadWrite.aspx
Kirtan Patel replied to Srinivasan S on 16-Nov-11 11:16 PM
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);
 
XmlNode productsNode = doc.CreateElement("products");
doc.AppendChild(productsNode);
 
XmlNode productNode = doc.CreateElement("product");
XmlAttribute productAttribute = doc.CreateAttribute("id");
productAttribute.Value = "01";
productNode.Attributes.Append(productAttribute);
productsNode.AppendChild(productNode);
 
XmlNode nameNode = doc.CreateElement("Name");
nameNode.AppendChild(doc.CreateTextNode("Java"));
productNode.AppendChild(nameNode);
XmlNode priceNode = doc.CreateElement("Price");
priceNode.AppendChild(doc.CreateTextNode("Free"));
productNode.AppendChild(priceNode);
 
// Create and add another product node.
productNode = doc.CreateElement("product");
productAttribute = doc.CreateAttribute("id");
productAttribute.Value = "02";
productNode.Attributes.Append(productAttribute);
productsNode.AppendChild(productNode);
nameNode = doc.CreateElement("Name");
nameNode.AppendChild(doc.CreateTextNode("C#"));
productNode.AppendChild(nameNode);
priceNode = doc.CreateElement("Price");
priceNode.AppendChild(doc.CreateTextNode("Free"));
productNode.AppendChild(priceNode);
doc.Save(Console.Out);  
 
Jitendra Faye replied to Srinivasan S on 16-Nov-11 11:37 PM
Using XMLTesxtWriter class you can create xml.

Follow this code-

private void WriteXMLFileUsingValuesFromCombos()
{
  try
  {
    string sStartupPath = Application.StartupPath;    
    XmlTextWriter objXmlTextWriter = 
         new XmlTextWriter(sStartupPath +  @"\selected.xml",null);
    objXmlTextWriter.Formatting = Formatting.Indented;
    objXmlTextWriter.WriteStartDocument();
    objXmlTextWriter.WriteStartElement("MySelectedValues");
    objXmlTextWriter.WriteStartElement("BookName");
    objXmlTextWriter.WriteString(cboBookName.Text);
    objXmlTextWriter.WriteEndElement();
    objXmlTextWriter.WriteStartElement("ReleaseYear");
    objXmlTextWriter.WriteString(cboReleaseYear.Text);

    objXmlTextWriter.WriteEndElement();
    objXmlTextWriter.WriteStartElement("Publication");
    objXmlTextWriter.WriteString(cboPublication.Text); 
    objXmlTextWriter.WriteEndElement();
    objXmlTextWriter.WriteEndElement();
    objXmlTextWriter.WriteEndDocument();
    objXmlTextWriter.Flush();
    objXmlTextWriter.Close();
    MessageBox.Show("The following file has been successfully created\r\n" 
        + sStartupPath 
        + @"\selected.xml","XML",MessageBoxButtons.OK,
        MessageBoxIcon.Information );
  }
  catch(Exception ex)
  {
    MessageBox.Show(ex.ToString());
  }
}


Try this and let me know.