• C#对XML文件的操作


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Data.OleDb;
    using Microsoft.Office.Interop.Excel;
    using System.Xml;
    using System.Xml.XPath;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //使用XmlReader读取Xml文件  *******************************************************************
                string xmlPath = "\books.xml";
    
                XmlReader rdr = XmlReader.Create(xmlPath);
    
                while (rdr.Read())
                {
                    if (rdr.NodeType == XmlNodeType.Element)
                    {
                        for (int i = 0; i < rdr.AttributeCount; i++)
                        {
                            Console.WriteLine(rdr.GetAttribute("genre") + "
    ");
                        }
                    }
                }
    
                //使用XmlWriter创建新Xml文件  *******************************************************************
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.NewLineOnAttributes = true;
                XmlWriter writer = XmlWriter.Create("newbook.xml", settings);
    
                writer.WriteStartDocument();
    
                writer.WriteStartElement("book");
                writer.WriteAttributeString("genre", "Mystery");
                writer.WriteElementString("title", "Case of the Missing Cookie");
    
                writer.WriteStartElement("author", "abc");
                writer.WriteElementString("name", "Cookie Monster");
                writer.WriteEndElement();
    
                writer.WriteElementString("price", "9.99");
                writer.WriteEndElement();
    
                writer.WriteEndDocument();
    
                writer.Flush();
                writer.Close();
    
                //使用XmlDocument读取Xml文件  *******************************************************************
                XmlDocument _doc = new XmlDocument();                            
                _doc.Load(xmlPath);                            
                XmlNodeList nodeList = _doc.GetElementsByTagName("title");                            
                for (int i = 0; i < nodeList.Count; i++)                            
                {                            
                    Console.WriteLine(nodeList[i].OuterXml + "
    ");                            
                }                            
                                
                Console.Read();                            
                                
                //XPath读取                        
                //XmlNodeList nodeList = _doc.selectNodes("bookstore/book/title");                        
    
                //使用XmlDocument方式插入节点到XML文件  *******************************************************************
                _doc = new XmlDocument();
                _doc.Load(xmlPath);
    
                XmlNode node = _doc.SelectSingleNode("bookstore");
    
                XmlElement newNode = _doc.CreateElement("book");
                newNode.SetAttribute("genre", "yuandong");
                newNode.SetAttribute("publicationdate", "2016");
                newNode.SetAttribute("ISBN", "1-5698-26-66");
                node.AppendChild(newNode);
    
                XmlElement newTitleNode = _doc.CreateElement("title");
                newTitleNode.InnerText = "The good day";
                newNode.AppendChild(newTitleNode);
    
                XmlElement newName = _doc.CreateElement("name");
                newName.InnerText = "MY";
    
                XmlElement newAuthorNode = _doc.CreateElement("author");
                newAuthorNode.AppendChild(newName);
    
                newNode.AppendChild(newAuthorNode);
    
                XmlElement newPrice = _doc.CreateElement("price");
                newPrice.InnerText = "89.9";
                newNode.AppendChild(newPrice);
    
                _doc.Save(xmlPath);
    
                //使用XPathNavigator类读取XML文件 *******************************************************************
                string strPath = AppDomain.CurrentDomain.BaseDirectory + "\books.xml";
    
                XPathDocument _doc2 = new XPathDocument(strPath);
                XPathNavigator nav = _doc2.CreateNavigator();
                XPathNodeIterator iter = nav.Select("/bookstore/book[@genre='novel']");
                while (iter.MoveNext())
                {
                    XPathNodeIterator newIter = iter.Current.SelectDescendants(XPathNodeType.Element, false);
                    while (newIter.MoveNext())
                    {
                        Console.WriteLine("Name :" + newIter.Current.Name + ",Value :" + newIter.Current.Value + "
    ");
                    }
                }
    
                Console.Write(nav.Evaluate("sum(/bookstore/book/price)").ToString());
    
                //使用XPathNavigator类插入节点  *******************************************************************
                XmlDocument doc = new XmlDocument();
                doc.Load("books.xml");
                XPathNavigator nav2 = doc.CreateNavigator();
    
                if (nav2.CanEdit)
                {
                    XPathNodeIterator iter2 = nav.Select("/bookstore/book/price");
                    while (iter.MoveNext())
                    {
                        iter2.Current.InsertAfter("<disc>5</disc>");
                    }
                    doc.Save("nwebooks.xml");
                }            
            }
        }
    }
  • 相关阅读:
    Codeforces461E Appleman and a Game 做题心得
    关于贪心问题的处理
    各种容斥 笔记
    博弈论 笔记
    家庭多个路由器组网方案(AP+AC)
    21.06.06 训练赛
    Redirect...
    Web Api实践系列(三)route特性使用
    《Red Tuner》隐私政策
    前缀和-1915. 最美子字符串的数目
  • 原文地址:https://www.cnblogs.com/YuanDong1314/p/8968307.html
Copyright © 2020-2023  润新知