• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            我希望你是我獨家記憶

            一段永遠封存的記憶,隨風(fēng)而去
            posts - 263, comments - 31, trackbacks - 0, articles - 3
               :: 首頁 :: 新隨筆 ::  :: 聚合  :: 管理

            XML簡單操作

            Posted on 2009-07-09 16:28 Hero 閱讀(150) 評論(0)  編輯 收藏 引用 所屬分類: C#積累

             

              1 using System;
              2 using System.Collections.Generic;
              3 using System.ComponentModel;
              4 using System.Data;
              5 using System.Drawing;
              6 using System.Text;
              7 using System.Windows.Forms;
              8 
              9 using System.Xml;
             10 
             11 namespace XML_print
             12 {
             13     public partial class Form1 : Form
             14     {
             15         public Form1()
             16         {
             17             InitializeComponent();
             18         }
             19 
             20         private void button_Loop_Click( object sender, EventArgs e )
             21         {
             22             //首先清空ListBox
             23             this.listBox_XMLList.Items.Clear();
             24 
             25             //載入XML文檔
             26             XmlDocument xmldoc = new XmlDocument();
             27             xmldoc.Load( @"E:\xml.xml" );
             28 
             29             //遞歸遍歷文檔
             30             RecurseXmlDocument( (XmlNode)xmldoc.DocumentElement, 0 );
             31         }
             32 
             33         private void RecurseXmlDocument( XmlNode root, int deep )
             34         {
             35             if ( null == root ) return;
             36 
             37             if( root is XmlElement )
             38             {
             39                 //首先打印名字
             40                 listBox_XMLList.Items.Add( root.Name.PadLeft( root.Name.Length + deep ) );
             41 
             42                 //遞歸調(diào)用
             43                 if( root.HasChildNodes )
             44                     RecurseXmlDocument( root.FirstChild, deep + 2 ) ;
             45 
             46                 if ( root.NextSibling != null )
             47                     RecurseXmlDocument( root.NextSibling, deep );
             48             }
             49             else if( root is XmlText )
             50             {
             51                 //打印文本
             52                 string text = ( (XmlText)root ).Value;
             53                 listBox_XMLList.Items.Add( text.PadLeft( text.Length + deep ) );
             54             }
             55             else if( root is XmlComment )
             56             {
             57                 string text = root.Value;
             58                 listBox_XMLList.Items.Add( text.PadLeft( text.Length + deep ) );
             59 
             60                 if ( root.HasChildNodes )
             61                     RecurseXmlDocument( root.FirstChild, deep + 2 );
             62 
             63                 if ( root.NextSibling != null )
             64                     RecurseXmlDocument( root.NextSibling, deep );
             65             }
             66         }
             67 
             68         private void Form1_Load( object sender, EventArgs e )
             69         {
             70 
             71         }
             72 
             73         private void button_Create_Click( object sender, EventArgs e )
             74         {
             75             //首先讀取文檔
             76             XmlDocument xmldoc = new XmlDocument();
             77             xmldoc.Load( @"E:/xml.xml" );
             78 
             79             //獲取根節(jié)點
             80             XmlElement root = xmldoc.DocumentElement;
             81 
             82             //創(chuàng)建新節(jié)點
             83             XmlElement newBook = xmldoc.CreateElement( "book" );
             84             XmlElement newTitle = xmldoc.CreateElement( "title" );
             85             XmlElement newAuthor = xmldoc.CreateElement( "author" );
             86             XmlElement newCode = xmldoc.CreateElement( "code" );
             87 
             88             XmlText title = xmldoc.CreateTextNode( "X" );
             89             XmlText author = xmldoc.CreateTextNode( "xxx" );
             90             XmlText code = xmldoc.CreateTextNode( "11111" );
             91 
             92             XmlComment commont = xmldoc.CreateComment( "this is a commont" );
             93 
             94             //插入這個元素
             95             newBook.AppendChild( commont );
             96             newBook.AppendChild( newTitle );
             97             newBook.AppendChild( newAuthor );
             98             newBook.AppendChild( newCode );
             99 
            100             newTitle.AppendChild( title );
            101             newAuthor.AppendChild( author );
            102             newCode.AppendChild( code );
            103 
            104             root.InsertAfter( newBook, root.FirstChild );
            105 
            106             //保存XML文檔
            107             xmldoc.Save( @"E:/xml.xml" );
            108         }
            109     }
            110 }
            久久久久国产视频电影| 国产精品久久久久国产A级| 亚洲国产精品久久| 久久国产V一级毛多内射| 欧美无乱码久久久免费午夜一区二区三区中文字幕 | 久久免费99精品国产自在现线 | 很黄很污的网站久久mimi色| 色综合久久天天综线观看| 久久综合亚洲鲁鲁五月天| 人妻无码αv中文字幕久久琪琪布 人妻无码久久一区二区三区免费 人妻无码中文久久久久专区 | 人妻无码αv中文字幕久久琪琪布| 久久人人超碰精品CAOPOREN| 久久久久高潮综合影院| 青青青国产精品国产精品久久久久| 久久中文精品无码中文字幕| 日韩人妻无码一区二区三区久久 | 欧美日韩久久中文字幕| 99久久这里只有精品| 亚洲精品视频久久久| segui久久国产精品| 99久久精品免费看国产一区二区三区 | 久久精品国产欧美日韩| 久久亚洲AV成人无码| 久久久91人妻无码精品蜜桃HD| 中文字幕久久精品无码| 色偷偷91久久综合噜噜噜噜| 国产精品成人99久久久久 | 久久99精品久久久久久水蜜桃 | 一本色道久久综合| 久久99国产一区二区三区| 99久久99久久精品国产| 青青草原1769久久免费播放| 久久久久亚洲AV片无码下载蜜桃| 偷窥少妇久久久久久久久| 亚洲午夜精品久久久久久app| 午夜福利91久久福利| 精品久久久久久久久久中文字幕| 亚洲国产精品久久| 久久精品国产72国产精福利| 国产精品久久久久久久午夜片| 色综合久久最新中文字幕|