免费男女视频_国产系列 视频二区_羞羞视频免费入口网站_久国久产久精永久网页_国产免费观看av_一区二区三区日韩在线观看

解析XML【C#】

1.XML元素
XML元素包含一個開標記、元素中的數據、閉標記
例如:<book>book name</book>
其中book是元素名稱  book name是元素數據
元素名稱區分大小寫
每一個XML文檔中必須有一個根元素
2.XML屬性
屬性添加在元素的開標記內
<book titile = "book name"></book>或者<book title = 'book name'></book>
屬性的值可以用雙引號也可以用單引號
3.元素與屬性舉例
<book><title>book name</title></book>
<book title = "book name"></book>
上述兩種沒有本質的區別,但使用時建議使用第一種,因為可以對元素增加屬性或子元素,但是不能這樣操作屬性。
4.XML讀取解析
DOM解析XML文檔:XML的文檔結構是個樹狀結構
DOM的類在System.Xml中
XmlDocument:用于加載XML文件存放的磁盤位置
XmlNode:這個類表示文檔書中的一個節點
XmlNodeList:表示一個節點的集合
XmlElement:表示XML文檔中的一個元素
XmlAttribute:表示XML的一個屬性
XmlText:表示開標記和閉標記之間的文本
(1)XmlDocument:加載xml文檔
例:XmlDocument document = new XmlDocument();
    document.load(@"D:\C#\books.xml");
(2)XmlElement:用于返回一個元素實例
   XmlElement element = document.DocumentElement; //常用來返回一個根節點
   XmlElement類包含許多方法和屬性,可以處理樹的節點和屬性
   1)FirstChild:返回根元素節點后的第一個子節點
   2)LastChild:返回根元素節點后的最后一個子節點
   3)ParentNode:返回當前元素的父節點
   4)NextSibling:返回當前節點的父節點下的下一個節點
   5)HasChildNodes:用來檢查當前元素是否有子元素

(3)XmlText:表示開關標記之間的文本,是特殊的節點,屬性有:
   1)InnerText:獲取當前節點范圍內的所有子節點的文本連接起來的字符串
   2)InnerXml:獲取當前節點范圍內的所有子節點的文本連接起來的字符串,包含標記

5.XML修改解析
 (1)創建節點

   創建節點方法:
   1)CreateElement:用來創建XmlDocument類型的節點
   2)CreateAttribute:用來創建XmlAttribute類型的節點
   3)CreateTextNode:用來創建XmlTextNode類型的節點
   4)CreateNode:用來創建任意類型的節點,包含上述三種以及其他
   創建節點后添加操作:
   1)AppendChild:把創建的節點類型追加到XmlNode類型或其他派生類型的節點后
   2)InsertAfter:將新節點插入到特定的位置
   3)InsertBefore:將新節點插入到特定位置
 (2)刪除節點
  1)RemoveChild:刪除當前節點范圍內的一個指定的子節點
  2)RemoveAll:刪除該節點范圍內的所有子節點
 (3)修改節點
  1)ReplaceChild:用新子節點替換舊子節點

6.XML查詢解析
 (1)使用XmlDocument 方法

   XmlDocument document = new XmlDocument();
   document.Load(filePath);
   XmlNodeList list = document.GetElementsByTagName("book");
   foreach (XmlNode node in list)
   {
     listBox1.Items.Add(node.Name);//listBox1類型是ListBox
   }
 (2)使用XPath選擇特定的節點的方法
  1)SelectSingleNode:用來選擇一個節點,如果有多個,則返回第一個節點
  2)SelectNodes:返回一個節點的集合,類型是XmlNodesList
 選擇特定的節點的方法參數XPath
  XPath是XML文檔的查詢語言

7.XML解析代碼

  1. using System;  
  2. using System.Data;  
  3. using System.Drawing;  
  4. using System.Text;  
  5. using System.Windows.Forms;  
  6. using System.IO;  
  7. using System.Xml;  
  8.   
  9. namespace Update  
  10. {  
  11.     public partial class Form1 : Form  
  12.     {  
  13.   
  14.         string filePath = Directory.GetCurrentDirectory() + @"\uploadXML.xml";  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.   
  20.         //讀取xml   
  21.         private void button1_Click(object sender, EventArgs e)  
  22.         {  
  23.   
  24.             if (File.Exists(filePath))  
  25.             {  
  26.   
  27.                 XmlDocument document = new XmlDocument();  
  28.                 document.Load(filePath);  
  29.                 XmlNode root = (XmlNode)document.DocumentElement;  
  30.                 //循環展示成樹狀   
  31.                 xmlProcess(root,0);  
  32.   
  33.             }  
  34.             
  35.         }  
  36.           
  37.   
  38.         /* 
  39.          * 循環展示成樹狀 
  40.          */  
  41.         private void xmlProcess(XmlNode root,int i)   
  42.         {  
  43.   
  44.             if (root == null)   
  45.             {  
  46.                 return;  
  47.             }  
  48.             if (root is XmlElement)   
  49.             {  
  50.                 listBox1.Items.Add(root.Name.PadLeft(root.Name.Length + i));  
  51.                 if (root.HasChildNodes)   
  52.                 {  
  53.                     xmlProcess(root.FirstChild,i+2);  
  54.                 }  
  55.                 if (root.NextSibling != null)   
  56.                 {  
  57.                     xmlProcess(root.NextSibling, i);  
  58.                 }  
  59.   
  60.             }  
  61.             else if (root is XmlText)   
  62.             {  
  63.                 string text = ((XmlText)root).Value;  
  64.                 listBox1.Items.Add(text.PadLeft(text.Length+i));  
  65.             }  
  66.         }  
  67.   
  68.         //讀取文本節點   
  69.         private void button2_Click(object sender, EventArgs e)  
  70.         {  
  71.             XmlDocument document = new XmlDocument();  
  72.             document.Load(filePath);  
  73.             XmlNode root = (XmlNode)document.DocumentElement;  
  74.             string values = root.InnerText;  
  75.             string values2 = root.InnerXml;  
  76.             string values3 = root.Value;  
  77.             MessageBox.Show(values);  
  78.             MessageBox.Show(values2);  
  79.             MessageBox.Show(values3);  
  80.         }  
  81.         //添加節點   
  82.         private void button3_Click(object sender, EventArgs e)  
  83.         {  
  84.             XmlDocument document = new XmlDocument();  
  85.             document.Load(filePath);  
  86.             XmlElement root = document.DocumentElement;  
  87.   
  88.             //create node   
  89.             XmlElement newBook = document.CreateElement("book");  
  90.             XmlElement newTitle = document.CreateElement("title");  
  91.             XmlElement newAuthor = document.CreateElement("author");  
  92.               
  93.             //create text   
  94.             XmlText titleText = document.CreateTextNode("new title");  
  95.             XmlText authorText = document.CreateTextNode("new author");  
  96.   
  97.             //insert   
  98.             newTitle.AppendChild(titleText);  
  99.             newAuthor.AppendChild(authorText);  
  100.   
  101.             newBook.AppendChild(newTitle);  
  102.             newBook.AppendChild(newAuthor);  
  103.   
  104.             root.AppendChild(newBook);  
  105.   
  106.             //save   
  107.             document.Save(filePath);  
  108.   
  109.   
  110.   
  111.   
  112.         }  
  113.         //刪除節點   
  114.         private void button4_Click(object sender, EventArgs e)  
  115.         {  
  116.             XmlDocument document = new XmlDocument();  
  117.             document.Load(filePath);  
  118.             XmlElement root = document.DocumentElement;  
  119.             if (root.HasChildNodes)   
  120.             {  
  121.                 XmlNode old = root.LastChild;  
  122.                 root.RemoveChild(old);  
  123.             }  
  124.   
  125.             document.Save(filePath);  
  126.   
  127.         }  
  128.         //修改節點   
  129.         private void button5_Click(object sender, EventArgs e)  
  130.         {  
  131.             XmlDocument document = new XmlDocument();  
  132.             document.Load(filePath);  
  133.             XmlElement root = document.DocumentElement;  
  134.             if (root.HasChildNodes)  
  135.             {  
  136.                 XmlNode oldNode = root.LastChild;  
  137.                 XmlNode newNode = document.CreateElement("Book");  
  138.                 root.ReplaceChild(newNode, oldNode);  
  139.             }  
  140.   
  141.             document.Save(filePath);  
  142.   
  143.         }  
  144.         //查詢節點   
  145.         private void button6_Click(object sender, EventArgs e)  
  146.         {  
  147.             listBox1.Items.Clear();  
  148.   
  149.             XmlDocument document = new XmlDocument();  
  150.             document.Load(filePath);  
  151.             XmlNodeList list = document.GetElementsByTagName("book");//要查詢的的節點名   
  152.             foreach (XmlNode node in list)   
  153.             {  
  154.                 listBox1.Items.Add(node.Name);  
  155.             }  
  156.         }  
  157.   
  158.     }  
  159. }  
using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace Update
{
    public partial class Form1 : Form
    {

        string filePath = Directory.GetCurrentDirectory() + @"\uploadXML.xml";
        public Form1()
        {
            InitializeComponent();
        }

        //讀取xml
        private void button1_Click(object sender, EventArgs e)
        {

            if (File.Exists(filePath))
            {

                XmlDocument document = new XmlDocument();
                document.Load(filePath);
                XmlNode root = (XmlNode)document.DocumentElement;
                //循環展示成樹狀
                xmlProcess(root,0);

            }
          
        }
        

        /*
         * 循環展示成樹狀
         */
        private void xmlProcess(XmlNode root,int i) 
        {

            if (root == null) 
            {
                return;
            }
            if (root is XmlElement) 
            {
                listBox1.Items.Add(root.Name.PadLeft(root.Name.Length + i));
                if (root.HasChildNodes) 
                {
                    xmlProcess(root.FirstChild,i+2);
                }
                if (root.NextSibling != null) 
                {
                    xmlProcess(root.NextSibling, i);
                }

            }
            else if (root is XmlText) 
            {
                string text = ((XmlText)root).Value;
                listBox1.Items.Add(text.PadLeft(text.Length+i));
            }
        }

        //讀取文本節點
        private void button2_Click(object sender, EventArgs e)
        {
            XmlDocument document = new XmlDocument();
            document.Load(filePath);
            XmlNode root = (XmlNode)document.DocumentElement;
            string values = root.InnerText;
            string values2 = root.InnerXml;
            string values3 = root.Value;
            MessageBox.Show(values);
            MessageBox.Show(values2);
            MessageBox.Show(values3);
        }
        //添加節點
        private void button3_Click(object sender, EventArgs e)
        {
            XmlDocument document = new XmlDocument();
            document.Load(filePath);
            XmlElement root = document.DocumentElement;

            //create node
            XmlElement newBook = document.CreateElement("book");
            XmlElement newTitle = document.CreateElement("title");
            XmlElement newAuthor = document.CreateElement("author");
            
            //create text
            XmlText titleText = document.CreateTextNode("new title");
            XmlText authorText = document.CreateTextNode("new author");

            //insert
            newTitle.AppendChild(titleText);
            newAuthor.AppendChild(authorText);

            newBook.AppendChild(newTitle);
            newBook.AppendChild(newAuthor);

            root.AppendChild(newBook);

            //save
            document.Save(filePath);




        }
        //刪除節點
        private void button4_Click(object sender, EventArgs e)
        {
            XmlDocument document = new XmlDocument();
            document.Load(filePath);
            XmlElement root = document.DocumentElement;
            if (root.HasChildNodes) 
            {
                XmlNode old = root.LastChild;
                root.RemoveChild(old);
            }

            document.Save(filePath);

        }
        //修改節點
        private void button5_Click(object sender, EventArgs e)
        {
            XmlDocument document = new XmlDocument();
            document.Load(filePath);
            XmlElement root = document.DocumentElement;
            if (root.HasChildNodes)
            {
                XmlNode oldNode = root.LastChild;
                XmlNode newNode = document.CreateElement("Book");
                root.ReplaceChild(newNode, oldNode);
            }

            document.Save(filePath);

        }
        //查詢節點
        private void button6_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();

            XmlDocument document = new XmlDocument();
            document.Load(filePath);
            XmlNodeList list = document.GetElementsByTagName("book");//要查詢的的節點名
            foreach (XmlNode node in list) 
            {
                listBox1.Items.Add(node.Name);
            }
        }

    }
}
主站蜘蛛池模板: 九九热这里只有精品8 | 精品国产中文字幕 | 成人毛片免费播放 | www.三区| japanesexxxx24videofree| 法国极品成人h版 | 国产美女视频一区二区三区 | 久草视频中文 | 免费专区 - 91爱爱 | www.48xx.com| 久久经典视频 | 久久精品成人影院 | 精品久久一区二区三区 | 国产午夜精品久久久久久久蜜臀 | 羞羞色院91精品网站 | 内地av在线 | 久久国产成人午夜av浪潮 | 欧洲色阁中文字幕 | 涩涩操| 亚洲视频在线网 | 一区二区三区四区视频在线观看 | 欧美a∨一区二区三区久久黄 | 一级国产精品一级国产精品片 | 91成人一区| 欧美一级黄色片在线观看 | 欧美精品一区二区久久 | 激情宗合网 | 久草在线视频精品 | 国产91在线播放九色 | 久久一区三区 | 日本中文视频 | 91成人亚洲 | 91精品国产777在线观看 | 免费黄色入口 | 欧美日韩手机在线观看 | asiass极品裸体女pics | 高清视频一区二区 | cosplay裸体福利写真 | 日本免费一区二区三区四区 | 久久久国产精品网站 | 在线播放免费播放av片 |