C#将数据添加到xml文件 [英] C# Adding data to xml file

查看:90
本文介绍了C#将数据添加到xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个部件应用程序,以学习C#和WPF。我尝试使用XmlWriter添加新零件时遇到麻烦。我可以创建xml文件,但是无法确定如何添加其他部分。我应该使用XmlDocument之类的东西吗?这是我后面的代码:

I'm building an Parts app in order to learn C# and WPF. I trying having trouble adding new parts using XmlWriter. I can create the xml file, but can not figure how to add additional parts. Should I be using something else like XmlDocument? Here is my code behind:

private void btnSave_Click(object sender, RoutedEventArgs e)
    {

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Encoding = Encoding.UTF8;
        settings.Indent = true;

        using (XmlWriter writer = XmlWriter.Create("f:\\MyParts.xml", settings))
        {
            writer.WriteStartDocument();
            writer.WriteStartElement("MyParts");
            writer.WriteStartElement("parts");
            writer.WriteStartElement("item");
            writer.WriteString(txtbxitem.Text);
            writer.WriteEndElement();

            writer.WriteStartElement("color");
            writer.WriteString(txtbxcolor.Text);
            writer.WriteEndElement();

            writer.WriteStartElement("size");
            writer.WriteString(txtbxsize.Text);
            writer.WriteEndElement();

            writer.WriteEndElement();
            writer.WriteEndDocument();

            writer.Flush();
            writer.Close();

        }
    }

此代码创建xml文件,并一个节点正确,但是如何添加其他零件?这是我要创建的内容:

This code creates the xml file and a node correctly, but how do I add additional parts? Here is what I am trying to create:

 <?xml version="1.0" encoding="ISO-8859-1" ?> 

<MyParts>
  <parts>
    <item>Part1</item>
    <color>Red</color>
    <size>SM</size>
  </parts>
  <parts>
    <item>Part2</item>
    <color>Blue</color>
    <size>XXL</size>
  </parts>
</MyParts>


推荐答案

我个人建议使用LINQ to XML。它是一个比 XmlDocument 更容易使用的API。

Personally I'd suggest using LINQ to XML. It's a much easier API to use than XmlDocument.

但是,如果您想要修改现有文档,然后通常使用内存中表示方法比使用流API更简单。当然可以做到后者,但这并不容易。

But yes, if you want to modify an existing document then typically using an in-memory representation is simpler than using a streaming API. It's possible to do the latter of course, but it's not easy.

以下是创建与您已经拥有的XML相同的示例(除了声明:您要想要使用Latin-1而不是像UTF-8这样可以代表整个Unicode的东西,btw吗?)

Here's an example to create the same XML as you've already got (other than the declaration: any reason you'd want to use Latin-1 instead of something like UTF-8 which can represent the whole of Unicode, btw?)

var doc = new XDocument(
  new XElement("MyParts",
    new XElement("parts",
      new XElement("item", "Part1"),
      new XElement("color", "Red"),
      new XElement("size", "SM")),
    new XElement("parts",
      new XElement("item", "Part2"),
      new XElement("color", "Blue"),
      new XElement("size", "XXL"))));

然后,如果您想添加另一部分:

Then if you wanted to add another part:

doc.Root.Add(
  new XElement("parts",
    new XElement("item", "Part3"),
    new XElement("color", "Green"),
    new XElement("size", "L")));

诚然,我希望您希望将创建部件元素位封装到避免一直重复的方法...但是希望您能得到照片。

Admittedly I'd expect you'd want to encapsulate the "create a parts element" bit into a method to avoid repeating it all the time... but hopefully you get the picture.

这篇关于C#将数据添加到xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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