Author Topic: writing data to XML file  (Read 4683 times)

0 Members and 1 Guest are viewing this topic.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
writing data to XML file
« 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
Code: [Select]
        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");
        }
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: writing data to XML file
« Reply #1 on: October 03, 2006, 10:09:52 AM »
So should I assume by the lack of response that no one is using XML?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

DBARANAS

  • Guest
Re: writing data to XML file
« Reply #2 on: October 03, 2006, 10:47:11 AM »
So should I assume by the lack of response that no one is using XML?

This is what I do.


Code: [Select]
    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

Draftek

  • Guest
Re: writing data to XML file
« Reply #3 on: October 03, 2006, 11:13:55 AM »
I ususally use the XMLDocument (DOM) Load and Save myself.

There are so many options it probably boils down the use and personal preference.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: writing data to XML file
« Reply #4 on: October 03, 2006, 02:05:46 PM »
I ususally use the XMLDocument (DOM) Load and Save myself.
do you have a small example?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Draftek

  • Guest
Re: writing data to XML file
« Reply #5 on: October 03, 2006, 04:00:02 PM »
sure, here is a routine I use for saving favorites:

Code: [Select]
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.

mohnston

  • Bull Frog
  • Posts: 305
  • CAD Programmer
Re: writing data to XML file
« Reply #6 on: October 04, 2006, 12:44:48 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.

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.
It's amazing what you can do when you don't know what you can't do.
CAD Programming Solutions