首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

关于C#查寻和添加(XML)

2013-07-04 
关于C#查找和添加(XML)昨天弄了差不多一晚上(我刚接触C#),现在问个关于C#查找和添加节点的问题:我有两个XM

关于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();


                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

热点排行