帮助附加到现有XML文件 [英] Help Appending to an existing XML file

查看:87
本文介绍了帮助附加到现有XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习如何在vb.net中使用XML文件,并且我在将新数据附加到现有xml文件时遇到了问题。



XML Layout

I have been learning how to work with XML files in vb.net and Im having issues appending new data to an existing xml file.

XML Layout

<HelpBox>
  <Categorys>
    <Category Selected="True" Name="Admin Tools"></Category>
    <Category Selected="False" Name="Scripts"></Category>
    <Category Selected="False" Name="Clipboard"></Category>
  </Categorys>
  <Actions>
    <Item Name="RDC" Icon="1" TaskType="Launch" Category="Admin Tools">
      <File>C:\Windows\System32\mstsc.exe</File>
      <Arg />
    </Item>
    <Item Name="MSRA" Icon="2" TaskType="Launch" Category="Admin Tools">
      <File>C:\Windows\System32\msra.exe</File>
      <Arg>offerra</Arg>
    </Item>
  </Actions>
</HelpBox>





现在我遇到的问题是在分类和操作下添加新项目。如果有人可以提供一个真正有用的例子。我一直在谷歌搜索过去2天,我还没有找到清楚显示如何追加的示例。



Now the issues I'm having are appending new items under Categorys and Actions. If someone could provide an example that would really help out. I have been searching google for the last 2 days and I haven't found and example that clearly shows how to append.

推荐答案

首先,问题一无所获与表单或任何其他类型的UI有关。现在,将任何文本附加到某些有效的XML是不可能的,在以下意义上:如果你这样做,文本将被转换为根据XML标准不能成为井的文本-formed XML。这是因为XML只能有一个根元素。



要获得一些结构良好的XML,你只能添加/插入一些XML作为孩子(红色)到现有的XML元素。为了操纵XML,.NET FCL提供了不同的方法。以下是我对它们的简短概述:

First of all, the problem has nothing to do with Forms, or any other kind of UI. Now, appending any text to some valid XML is impossible, in the following sense: if you do that, the text is transformed to the text which cannot be, according the XML standard, a well-formed XML. This is because XML can have only one root element.

To obtain some well-formed XML, you can only add/insert some XML as a child(red) to existing XML element. To manipulate XML, .NET FCL offers different approaches. Here is my short overview of them:
  1. 使用 System.Xml.XmlDocument 类。它实现了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


这篇关于帮助附加到现有XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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