Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Greetings,

I am new to C# and XML coding. I have read a few tutorials online about how to deal with XML files. Currently I have a setup that creates an XML file with a few products that are being saved. I would like to read the file that has already been created, add another product to it and then save it but currently my code only overwrites what I have. Does anyone know of the way to add on to an XML file when trying to write to it. Here's the example of my XML file.

XML
<table> 
   -<part> 
       <product_name>Test</product_name> 
       <length>10</length> <width>8</width> 
       <thickness>1.5</thickness> 
       <offset>.25</offset> 
       <yelements>6</yelements> 
       <xelements>7</xelements> 
    </part>
 </table>


Here's the code:

C#
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("product.xml");
            XmlTextWriter writer = new XmlTextWriter("product.xml", System.Text.Encoding.UTF8);
            writer.WriteStartDocument(true);
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 2;
            writer.WriteStartElement("Table");
            createNode(Name.Text, Cut_Area_X.Text, Cut_Area_Y.Text, Thickness.Text, Offset.Text, Number_Cuts_X.Text, Number_Cuts_Y.Text, writer);
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
            MessageBox.Show("XML File created ! ");
        }

        private void createNode(string Name, string Length, string Width, string Thickness, string offset, string YElements, string XElements, XmlTextWriter writer)
        {
            writer.WriteStartElement("Part");
            writer.WriteStartElement("Product_Name");
            writer.WriteString(Name);
            writer.WriteEndElement();
            writer.WriteStartElement("Length");
            writer.WriteString(Length);
            writer.WriteEndElement();
            writer.WriteStartElement("Width");
            writer.WriteString(Width);
            writer.WriteEndElement();
            writer.WriteStartElement("Thickness");
            writer.WriteString(Thickness);
            writer.WriteEndElement();
            writer.WriteStartElement("Offset");
            writer.WriteString(offset);
            writer.WriteEndElement();
            writer.WriteStartElement("YElements");
            writer.WriteString(YElements);
            writer.WriteEndElement();
            writer.WriteStartElement("XElements");
            writer.WriteString(XElements);
            writer.WriteEndElement();
            writer.WriteEndElement();

        }
    }
}
Posted
Updated 3-Jun-13 4:54am
v2

1 solution

You don't need to combine XmlDocument and XmlTextWriter (and how? no wonder it does not work). The pattern is this: XmlDocument represents XML DOM, which is populated, in this case, via XmlDocument.LoadXml. You should modify DOM using its API (see the documentation referenced below) and simply save the resulting document.

You can use some other approach to XML manipulations. This is my short overview of .NET FCL classes used to work with XML:


  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the classes System.Xml.XmlTextWriter and System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx[^], http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].


Good luck,
—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900