昨天弄了差不多一晚上(我刚接触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.   

    1.var xmlStr = @"<?xml version=""1.0"" encoding=""UTF-16""?>
    <!-- Microsoft Virtual Machine Options and Settings --><preferences>
    </preferences>
    ";
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.LoadXml(xmlStr);
                    bool isExist = xmlDoc.GetElementsByTagName("components").Count > 0;
                    if (!isExist)
                    {
                        XmlElement xe = xmlDoc.CreateElement("components");
                        XmlElement child_1 = xmlDoc.CreateElement("host_time_sync");
                        XmlElement child_2 = xmlDoc.CreateElement("enabled");
                        child_2.SetAttribute("type", "boolean");
                        child_2.InnerText = "false";
                        child_1.AppendChild(child_2);
                        xe.AppendChild(child_1);
                        xmlDoc.DocumentElement.AppendChild(xe);//加入根节点
                    }
    2.string xmlStr = @"<?xml version=""1.0""?>
    <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>
    </VirtualBox>";                XmlDocument xmlDoc = new XmlDocument();
                                   xmlDoc.LoadXml(xmlStr);
                                   XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
                                   nsmgr.AddNamespace("bk", "http://www.innotek.de/VirtualBox-settings");                               // Select the first book written by an author whose last name is Atwood.
                                   
                                   XmlElement root = xmlDoc.DocumentElement;
                                   XmlNode Machine = root.SelectSingleNode("descendant::bk:Machine", nsmgr);
                                   string name = Machine.Attributes["name"].Value;//Windows XP
      

  2.   


    第一个还是有问题
    这个字串被加到末尾了
    我的第一个XML(如果你用过Microsoft Virtual PC,可以打开它的配置文件看看)是这样的:
    <preferences>
    其他节点
        <integration>
            <microsoft>
    我希望在微软下添加这个子节点。
    第二个能正常使用。