TheSwamp
Code Red => .NET => Topic started by: David Hall on October 02, 2006, 02:47:14 PM
-
What is the best way to write to an XML file? I have been reading about StreamReader, which has a corresponding Streamwriter, but a friend here at work uses dataSet.WriteXML. I'm looking for the easiest/best practice to do this. here is example of what I'm doing
private void button1_Click(object sender, EventArgs e)
{
DataRow dr = dataSet1.Tables[0].NewRow();
dr.BeginEdit();
dr[0] = comboBox1.Text;
dr[1] = textBox1.Text;
dr[2] = textBox2.Text;
dr[3] = textBox3.Text;
dr.EndEdit();
dataSet1.Tables[0].Rows.Add(dr);
dataSet1.Tables[0].AcceptChanges();
dataSet1.WriteXml(@"c:\carlot.xml");
}
-
So should I assume by the lack of response that no one is using XML?
-
So should I assume by the lack of response that no one is using XML?
This is what I do.
Public Sub WriteFloorGrid()
Dim SR As New StreamWriter(SetupItems.ProjectPath & "FloorGrid.xml")
Dim xmlSerial As New Xml.Serialization.XmlSerializer(GridItems.GetType)
xmlSerial.Serialize(SR, GridItems)
SR.Close()
SR = Nothing
xmlSerial = Nothing
End Sub
Public Sub ReadFloorGrid()
Dim xmlSerial As New Xml.Serialization.XmlSerializer(GridItems.GetType)
Dim SR As New StreamReader(SetupItems.ProjectPath & "FloorGrid.xml")
GridItems = DirectCast(xmlSerial.Deserialize(SR), GridItem)
SR.Close()
SR = Nothing
xmlSerial = Nothing
End Sub
-
I ususally use the XMLDocument (DOM) Load and Save myself.
There are so many options it probably boils down the use and personal preference.
-
I ususally use the XMLDocument (DOM) Load and Save myself.
do you have a small example?
-
sure, here is a routine I use for saving favorites:
private void XMLToFavorites()
{
try
{
string sPath = Application.StartupPath + "\\Favorites.xml";
XmlDocument xmlDoc = new XmlDocument();
XmlElement oParent;
// need to create it and the parent node
if (!File.Exists(sPath))
{
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
// Create the root element
XmlElement rootNode = xmlDoc.CreateElement("Favorites");
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
xmlDoc.AppendChild(rootNode);
xmlDoc.Save(sPath);
// should exit here because there is nothing else to do
return;
}
xmlDoc.Load(sPath);
oParent = xmlDoc.DocumentElement;
Favorites = new System.Collections.ArrayList();
XmlNodeList oList = oParent.ChildNodes;
foreach (XmlNode xNode in oList)
{
// add each item here
System.Windows.Forms.ToolStripMenuItem mnuTemp
= new System.Windows.Forms.ToolStripMenuItem(xNode.InnerText);
mnuTemp.Click += new EventHandler(mnuTemp_Click);
mnuTemp.MouseDown += new MouseEventHandler(mnuTemp_MouseDown);
Favorites.Add(mnuTemp);
mnuFavorites.DropDownItems.Add(mnuTemp);
}
}
catch{}
}
// push the selected favorites to an xml file
private void FavoritesToXML()
{
try
{
string sPath = Application.StartupPath + "\\Favorites.xml";
XmlDocument xmlDoc = new XmlDocument();
// delete if it already exists
if (File.Exists(sPath))
{
File.Delete(sPath);
}
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
// Create the root element
XmlElement rootNode = xmlDoc.CreateElement("Favorites");
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
xmlDoc.AppendChild(rootNode);
foreach (ToolStripMenuItem tsItem in mnuFavorites.DropDownItems)
{
XmlNode xNode = xmlDoc.CreateNode(XmlNodeType.Element, "Favorite", "");
xNode.InnerText = tsItem.Text;
rootNode.AppendChild(xNode);
}
xmlDoc.Save(sPath);
}
catch{}
}
although, I did notice I had used the DataSet.WriteXML as you showed earlier when working directly with a dataset object.
-
What is the best way to write to an XML file? I have been reading about StreamReader, which has a corresponding Streamwriter, but a friend here at work uses dataSet.WriteXML. I'm looking for the easiest/best practice to do this.
Commander,
I use the dataSet.WriteXML method too. I make sure that I include the schema.
It is the "best" way that I have tried. However, it is such an easy method and works so well I haven't tried the others.