如何在C#中更新XML文件 [英] How do you update a XML file in C#

查看:158
本文介绍了如何在C#中更新XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,



我是C#和XML编码的新手。我在线阅读了一些关于如何处理XML文件的教程。目前,我有一个设置,可以创建一个XML文件,其中包含一些正在保存的产品。我想阅读已经创建的文件,添加另一个产品然后保存它,但目前我的代码只覆盖了我的内容。有没有人知道在尝试写入XML文件时添加到XML文件的方法。这是我的XML文件的示例。



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>





这是代码:< br $> b $ b



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();

        }
    }
}

推荐答案

您无需合并 XmlDocument XmlTextWriter (以及如何?难怪它不起作用)。模式是: XmlDocument 表示XML DOM,在这种情况下,通过 XmlDocument.LoadXml 填充。您应该使用其API修改DOM(请参阅下面引用的文档)并简单地保存生成的文档。



您可以使用其他一些XML操作方法。这是我用于处理XML的.NET FCL类的简短概述:



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. 使用 System.Xml.XmlDocument class。它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ]。
  2. 使用类 System.Xml.XmlTextWriter System.Xml.XmlTextReader ;这是最快的阅读方式,特别是你需要跳过一些数据。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx [ ^ ], http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ ^ ]。
  3. 使用类系统.Xml.Linq.XDocument ;这是类似于 XmlDocument 的最合适的方式,支持LINQ to XML Programming。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ],http://msdn.microsoft.com/en-us/library/bb387063.aspx [ ^ ]。

  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[^].





祝你好运,

-SA


这篇关于如何在C#中更新XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆