C#讀寫XML檔

		string bfilePath = @"E:\111" ;
          if (Directory.Exists(bfilePath) == false)
          {
            MessageBox.Show("指定文件夾不存在,請重新選擇路徑!");
            return;
         }
        try
        {
            string title = string.Empty;
            string author = string.Empty;

            //讀取XML文檔
            XmlDocument xmlDoc = new XmlDocument();
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;//忽略文档里面的注释
            XmlReader reader = XmlReader.Create(@"E:\111\Book2.xml", settings);
            xmlDoc.Load(reader);
            string books = xmlDoc.SelectSingleNode("bookstore").InnerText;  //SelectSingleNode()得到指定的结点 節點內的值 

            // 得到根节点bookstore
            XmlNode Con_Information = xmlDoc.SelectSingleNode("bookstore");
            // 得到根节点的所有子节点
            XmlNodeList Con_Info_List = Con_Information.ChildNodes;
            foreach (XmlNode xn1 in Con_Info_List)
            {
                title = xn1.SelectSingleNode("title").InnerText;
                author = xn1.SelectSingleNode("author").InnerText;
            }



            //生成XML文檔
            XmlDocument wxmlDoc = new XmlDocument();
            XmlDeclaration declaration1 = wxmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "");//xml文档的声明部分
            wxmlDoc.AppendChild(declaration1);

            XmlElement el = wxmlDoc.CreateElement("books"); //根元素
            el.SetAttribute("xmlns", @"http://xmlns.qualcomm.com/it/eng/stargate/assemblyData/v1");
            el.SetAttribute("xmlns:xsi", @"http://www.w3.org/2001/XMLSchema-instance");
            el.SetAttribute("version", "1.2");
            XmlAttribute xaa = wxmlDoc.CreateAttribute("schemaLocation", "http://www.w3.org/2001/XMLSchema-instance");
            xaa.Value = "http://xmlns.qualcomm.com/it/eng/stargate/assemblyData/v1 /qualcomm/it/eng/stargate/assemblyData/v1/AssemblyData-1_2.xsd";
            xaa.Prefix = "xsi";
            el.Attributes.Append(xaa);
            wxmlDoc.AppendChild(el);

            XmlElement head = wxmlDoc.CreateElement("header"); //子元素第一部分
            el.AppendChild(head);

            XmlElement Supplier = wxmlDoc.CreateElement("Supplier");
            Supplier.InnerText = "FALCON";
            head.AppendChild(Supplier);


            XmlElement booker = wxmlDoc.CreateElement("booker"); //子元素 第二部分
            el.AppendChild(booker);

            #region   
            //第二部分 子元素 的子元素
            XmlElement book = wxmlDoc.CreateElement("book1");
            booker.AppendChild(book);

            XmlElement title1 = wxmlDoc.CreateElement("title");  // <title>路由型与交换型互联网基础</title> 
            title1.InnerText = "路由型与交换型互联网基础";
            book.AppendChild(title1);

            XmlElement author1 = wxmlDoc.CreateElement("author");  // <author>程庆梅</author>
            title1.InnerText = "程庆梅";
            book.AppendChild(title1);

            // <TestDateTime start="1111" end="222" timeZone="Asia/Shanghai" />
            XmlElement TestDateTime = wxmlDoc.CreateElement("TestDateTime");
            TestDateTime.SetAttribute("start", "1111");
            TestDateTime.SetAttribute("end", "222");
            TestDateTime.SetAttribute("timeZone", "Asia/Shanghai");
            book.AppendChild(TestDateTime);

            #endregion

            string newFilePath = @"E:\111\WBook.xml";
            if (File.Exists(newFilePath))
            {
                File.Delete(newFilePath);
            }
            wxmlDoc.Save(newFilePath);
            
        }
        catch
        {
           
        }

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值