日期:2014-05-17  浏览次数:20998 次

关于C#查找和添加(XML)
昨天弄了差不多一晚上(我刚接触C#),现在问个关于C#查找和添加节点的问题:
我有两个XML文件
结构类似这样
第一个是:

<?xml version="1.0" encoding="UTF-16"?>
<!-- Microsoft Virtual Machine Options and Settings -->
<preferences>
.......
<integration>
<microsoft>

要求:
判断这个microsoft子节点下有无下面的节点,没有则新建下面的节点:

        <components> 
               <host_time_sync> 
                     <enabled type="boolean">false</enabled> 
               </host_time_sync> 
         </components> 

第二个文件:

<?xml version="1.0"?>
<!--
** DO NOT EDIT THIS FILE.
** If you make changes to this file while any VirtualBox related application
** is running, your changes will be overwritten later, without taking effect.
** Use VBoxManage or the VirtualBox Manager GUI to make changes.
-->
<VirtualBox xmlns="http://www.innotek.de/VirtualBox-settings" version="1.12-windows">
  <Machine uuid="{11019c7b-d81c-4ad3-afad-32b993da7ec6}" name="Windows XP" OSType="WindowsXP" snapshotFolder="Snapshots" lastStateChange="2013-07-01T23:50:02Z">

要求:
取出Machine中的"Name"的值,比如这个文件里面的是Windows XP
我实在没有什么好的思路了,所以来向各位高手请教,谢谢!

------解决方案--------------------
1.
var xmlStr = @"<?xml version=""1.0"" encoding=""UTF-16""?>
<!-- Microsoft Virtual Machine Options and Settings -->

<preferences>
</preferences>
";
                XmlDocument xmlDoc = new XmlDocument();