Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XML Visual-Studio LINQ
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.
 
<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:
 
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 3-Jun-13 5:41am
Edited 3-Jun-13 5:54am
Pheonyx11.8K
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.  
  3. 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[^].
  4.  
  5. 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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 255
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 184
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,665


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 3 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100