如何在现有的xml文件中编写 [英] how to write in an existing xml file

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

问题描述

大家好,

我已使用此代码读取现有的xml文件-

Hi Everyone,

I''ve used this code to read an existing xml file -

using (XmlReader reader = XmlReader.Create("XMLFile1.xml"))
{
  while (reader.Read())
  {
    switch (reader.NodeType)
    {
      case XmlNodeType.Element:
        if (reader.ReadToFollowing("title"))
        {
          output.AppendLine("Title: " + reader.ReadElementContentAsString());
          output.AppendLine();
        }
        if (reader.ReadToFollowing("first-name"))
        {
          output.AppendLine("First Name: " + reader.ReadElementContentAsString());
          output.AppendLine();
        }
        if (reader.ReadToFollowing("last-name"))
        {
          output.AppendLine("Last Name: " + reader.ReadElementContentAsString());
          output.AppendLine();
        }
        if (reader.ReadToFollowing("price"))
        {
          output.AppendLine("Price: " + reader.ReadElementContentAsString());
        }

        break;
    }
  }
  textblockTitle.Text = output.ToString();

  output.Remove(0, output.Length);
}




(其中输出是StringBuilder对象)

这是我的XML文件(XMLFile1.xml)





(where output is a StringBuilder object)

this is my XML file (XMLFile1.xml)


<!-- This is a sample XML document -->
<bookstore>
  <book genre="autobiography" publicationdate="1981-03-22" isbn="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
</bookstore>




现在我要写入/附加到此文件,
我该怎么办?
(XMLFile1.xml是现有的xml文件.)




Now i want to write/append to this file,
how could i do this?
(XMLFile1.xml is an existing xml file.)

推荐答案

请参见在附加数据时> THIS [ ^ ]线程到XML.这可能会对您有所帮助.
See THIS[^] thread on appending data to XML. This might help you.


请改用XmlDocument类.

http://msdn.microsoft.com/en-us/library/system.xml. xmldocument.aspx [ ^ ]

祝你好运!
Use the XmlDocument class instead.

http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^]

Good luck!


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

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