C# 写入XML

<?xml version="1.0" encoding="gb2312"?> 

<唐诗> 

<五言绝句> 

<作者 字号="太白">李白</作者> 

<标题>静夜思</标题> 

<内容>床前明月光,疑是地上霜。举头望明月,低头思故乡。</内容> 

</五言绝句> 

<五言绝句> 

<作者 字号="太白">李太白</作者> 

<标题>春晓</标题> 

<内容>春眠不觉晓,处处闻啼鸟。夜来风雨声,花落知多少。</内容> 

</五言绝句> 

<五言绝句> 

<作者 字号="季凌">王之涣</作者> 

<标题>登鹤雀楼</标题> 

<内容>白日依山尽,黄河入海流。欲穷千里目,更上一层楼</内容> 

</五言绝句> 

<五言绝句> 

<作者>李清照</作者> 

<标题>如梦令</标题> 

<内容>昨夜风疏雨骤,浓睡不消残酒,试问卷帘人,却道海棠依旧,知否,知否,应是绿肥红瘦。</内容> 

</五言绝句> 

</唐诗> 

作者:mjgforever
出处:http://mjgforever.cnblogs.com/ 
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明。

  

View Code
   1 要编写XML同样是采用流的概念,在.NET中编写XML的细节是作为XmlWriter类来实现的,但该类是抽象类不能够实例化,为此,我们要想在程序中访问它的方法以实现编写XML的愿望,必需使用它的派生类XmlTextWriter,该类提供了一系列的属性和方法为我们编写XML做准备,下面将详细的介绍这个类: 
   2 
   3 构造函数: 
   4 
   5 public XmlTextWriter(TextWriter); 
   6 
   7 public XmlTextWriter(Stream, Encoding); 
   8 
   9 public XmlTextWriter(string, Encoding); 
  10 
  11 第一个构造函数是把现有的TextWriter实例传递过去,System.IO.TextWriter类是一个有序的字符流 
  12 
  13 第二个构造函数是把要写入的流作为第一个参数,第二个参数是指定XML文档的编码方式,默认是UTF8,可取Encoding的枚举值,流可以是FileStream,MemoryStream,NetworkStream等等 
  14 
  15 第三个构造函数是把希望写入的文件名当作一个字符串(如果存在,就重写该文件)传递给第一个参数,第二个参数指定编码方式 
  16 
  17 
  18 
  19 常用的方法: 
  20 
  21 WriterStartDocument()和WriterEndDocument()方法: 
  22 
  23 第一个方法用来编写XML声明部分,如:<?xml version=”1.0” encoding=”UTF-8” ?> 
  24 
  25 第二个方法用来关闭任何打开的元素或属性并将编写器重新设置为 Start 状态。 
  26 
  27 
  28 
  29 WriterStartElement()和WriteEndElement()方法: 
  30 
  31 第一个方法用来写出指定的开始标记,该方法有以下几个重载: 
  32 
  33 WriterStartElement(string localname) 
  34 
  35 使用传递的字符串作为元素的本地名称 
  36 
  37 WriterStartElement(string localname,string namespace) 
  38 
  39 第一个参数指定元素的本地名称,第二个参数指定元素所在的命名空间 
  40 
  41 WriterStartElement(string prefix,string localname,string namespace) 
  42 
  43 第一个参数指定元素的前缀,第二个参数指定元素的本地名称,第三个参数指定元素所在的命名空间 
  44 
  45 第二个方法用来写出与开始元素对应的关闭元素,如果开始元素不包含任何内容,将用一个”/>”做为关闭元素 
  46 
  47 
  48 
  49 WriterStartAttribute()和WriterEndAttribute()方法: 
  50 
  51 第一个方法用于编写一个属性的开头,该方法有两个重载: 
  52 
  53 WriterStartAttribute(string localname,string namespace) 
  54 
  55 第一个参数指定属性的本地名称,第二个参数指定属性所在的命名空间 
  56 
  57 WriterStartAttribute(string prefix,string localname,string namespace) 
  58 
  59 第一个参数指定属性的前缀,第二个参数指定属性的本地名称,第三个参数指定属性所在的命名空间 
  60 
  61 第二个方法用于关闭WriterStartAttribute创建的属性 
  62 
  63 
  64 
  65 WriterElementString()方法: 
  66 
  67 该方法可以创建一个包含字符串值的元素,它有以下重载: 
  68 
  69 WriterElementString(string localname,string value) 
  70 
  71 如果编写这样的代码:WriterElementString(“para”,”Some text”) 将输出:<para>Some text</para> 
  72 
  73 WriterElementString(string localname,string namespace,string value) 
  74 
  75 如果编写这样的代码:WriterElementString(“para”,”http://www.w3.org/ns”,”Some text”) 将输出:<para xmlns=”http://www.w3.org/ns”>Some text</para> 
  76 
  77 如果编写嵌套几级的元素可使用WriterStartElement()和WriterEndElement()方法,如果编写直接包含内容的元素可以使用该方法 
  78 
  79 
  80 
  81 WriterAttributeString()方法: 
  82 
  83 类似与WriterElementString()方法,在使用上如果属性的值当中不包含实体可直接使用该方法来写出属性,如果属性值包含实体可使用WriterStartAttribute()和WriterEndAttribute()方法,例如要写出这样的XML——<para author=”Do&0241;a&amp;L.Perez”/>,可编写以下代码: 
  84 
  85 WriterStartElement(“para”); 
  86 
  87 WriterStartAttribute(“author”,null); 
  88 
  89 WriterString(“Do”); 
  90 
  91 WriterCharEntiry(“~n”); 
  92 
  93 WriterString(“a”); 
  94 
  95 WriterCharEntiry(“&”); 
  96 
  97 WriterString(“L.Perez”); 
  98 
  99 WriterEndAttribute(); 
 100 
 101 WriterEndElement(); 
 102 
 103 该方法有以下重载: 
 104 
 105 WriterAttributeString(string localname,string value); 
 106 
 107 WriterAttributeString(string localname,string namespace,string value); 
 108 
 109 WriterAttributeString(string prefx, string localname,string namespace,string value); 
 110 
 111 
 112 
 113 WriterNode(XmlReader reader,bool defattr)方法: 
 114 
 115 该方法可以从XmlReader读取器中复制节点并把它们写入XmlWriter流中,第一个参数是XmlReader的实例,第二个参数接受一个布尔值,决定是否复制元素中的属性,考虑下面XML片段: 
 116 
 117 <para> 
 118 
 119 <sent> 
 120 
 121 The<b>XmlWriter</b>class writes XML content to a Stream. 
 122 
 123 </sent> 
 124 
 125 </para> 
 126 
 127 以下代码复制其中的片段,reader代表XmlReader的实例writer代表XmlWriter类的实例: 
 128 
 129 while(reader.Read()) 
 130 
 131 { 
 132 
 133 if (reader.Name == ”sent” && reader.NodeType == XmlNodeType.Element) 
 134 
 135 { 
 136 
 137 writer.WriterNode(reader,true); 
 138 
 139 } 
 140 
 141 } 
 142 
 143 得到以下输出: 
 144 
 145 <sent> 
 146 
 147 The<b>XmlWriter</b>class writes XML content to a Stream. 
 148 
 149 </sent> 
 150 
 151 
 152 
 153 WriterComment(string text)方法:用于写出注释 
 154 
 155 WriterString(string text)方法:用于写出文本 
 156 
 157 WriterCData(string text)方法:写出CDATA数据块 
 158 
 159 WriterBase64(byte[] buffer,int index,int count)方法:将指定的二进制字节编码为 Base64 并写出结果文本 
 160 
 161 Flush():将缓冲区中的所有内容刷新到基础流,并同时刷新基础流 
 162 
 163 Close():关闭此流和基础流 
 164 
 165 
 166 
 167 以上对XmlTextWriter类的一些重要方法做了简单介绍,下面我们就来看一个例程,看看在程序中如何使用这些方法,照样还是先来看下运行效果图: 
 168 
 169 
 170 Example1按纽将向一个文件写出XML声明和一个元素节点以及节点内的文本,Example2按纽将在Example1的基础上添加属性节点,嵌套元素以及文本,WriteNode按纽使用WriterNode()方法在现有读取器中复制该读取器中的所有元素及属性并写到一个新的XML文档中,Example3按纽将写一份完整的XML文档,Example4按纽在Example3按纽的基础上另外生成一份文档并向该文档中追加CDATA部分,Example5按纽将使用WriterBase64()方法对一幅图片进行编码并将编码后的数据写到XML文档中,Example6按纽将使用Example5按纽中生成的XML读取其中数据并对其中编码数据进行解码最后生成一张图片。 
 171 
 172 以下是功能实现代码: 

 173 namespace XMLWriting 
 174 
 175 { 
 176 
 177 using System; 
 178 
 179 using System.IO; 
 180 
 181 using System.Text; 
 182 
 183 using System.Xml; 
 184 
 185 using System.Drawing; 
 186 
 187 using System.Collections; 
 188 
 189 using System.ComponentModel; 
 190 
 191 using System.Windows.Forms; 
 192 
 193 using System.Data; 
 194 
 195 
 196 
 197 /**//// <summary> 
 198 
 199 /// Form1 的摘要说明。 
 200 
 201 /// </summary> 
 202 
 203 public class Form1 : System.Windows.Forms.Form 
 204 
 205 { 
 206 
 207 private System.Windows.Forms.TextBox textBox1; 
 208 
 209 private System.Windows.Forms.Button button1; 
 210 
 211 private System.Windows.Forms.Button button2; 
 212 
 213 private System.Windows.Forms.Button button3; 
 214 
 215 private System.Windows.Forms.Button button4; 
 216 
 217 private System.Windows.Forms.Button button5; 
 218 
 219 private System.Windows.Forms.Button button6; 
 220 
 221 private System.Windows.Forms.Button button7; 
 222 
 223 /**//// <summary> 
 224 
 225 /// 必需的设计器变量。 
 226 
 227 /// </summary> 
 228 
 229 private System.ComponentModel.Container components = null; 
 230 
 231 
 232 
 233 public Form1() 
 234 
 235 { 
 236 
 237 // 
 238 
 239 // Windows 窗体设计器支持所必需的 
 240 
 241 // 
 242 
 243 InitializeComponent(); 
 244 
 245 
 246 
 247 // 
 248 
 249 // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 
 250 
 251 // 
 252 
 253 } 
 254 
 255 
 256 
 257 /**//// <summary> 
 258 
 259 /// 清理所有正在使用的资源。 
 260 
 261 /// </summary> 
 262 
 263 protected override void Dispose( bool disposing ) 
 264 
 265 { 
 266 
 267 if( disposing ) 
 268 
 269 { 
 270 
 271 if (components != null) 
 272 
 273 { 
 274 
 275 components.Dispose(); 
 276 
 277 } 
 278 
 279 } 
 280 
 281 base.Dispose( disposing ); 
 282 
 283 } 
 284 
 285 
 286 
 287 Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码 
 288 
 289 /**//// <summary> 
 290 
 291 /// 设计器支持所需的方法 - 不要使用代码编辑器修改 
 292 
 293 /// 此方法的内容。 
 294 
 295 /// </summary> 
 296 
 297 private void InitializeComponent() 
 298 
 299 { 
 300 
 301 this.textBox1 = new System.Windows.Forms.TextBox(); 
 302 
 303 this.button1 = new System.Windows.Forms.Button(); 
 304 
 305 this.button2 = new System.Windows.Forms.Button(); 
 306 
 307 this.button3 = new System.Windows.Forms.Button(); 
 308 
 309 this.button4 = new System.Windows.Forms.Button(); 
 310 
 311 this.button5 = new System.Windows.Forms.Button(); 
 312 
 313 this.button6 = new System.Windows.Forms.Button(); 
 314 
 315 this.button7 = new System.Windows.Forms.Button(); 
 316 
 317 this.SuspendLayout(); 
 318 
 319 // 
 320 
 321 // textBox1 
 322 
 323 // 
 324 
 325 this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
 326 
 327 | System.Windows.Forms.AnchorStyles.Left) 
 328 
 329 | System.Windows.Forms.AnchorStyles.Right))); 
 330 
 331 this.textBox1.Location = new System.Drawing.Point(0, 8); 
 332 
 333 this.textBox1.Multiline = true; 
 334 
 335 this.textBox1.Name = "textBox1"; 
 336 
 337 this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both; 
 338 
 339 this.textBox1.Size = new System.Drawing.Size(784, 332); 
 340 
 341 this.textBox1.TabIndex = 0; 
 342 
 343 this.textBox1.Text = ""; 
 344 
 345 // 
 346 
 347 // button1 
 348 
 349 // 
 350 
 351 this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); 
 352 
 353 this.button1.Location = new System.Drawing.Point(0, 344); 
 354 
 355 this.button1.Name = "button1"; 
 356 
 357 this.button1.TabIndex = 1; 
 358 
 359 this.button1.Text = "Example1"; 
 360 
 361 this.button1.Click += new System.EventHandler(this.button1_Click); 
 362 
 363 // 
 364 
 365 // button2 
 366 
 367 // 
 368 
 369 this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); 
 370 
 371 this.button2.Location = new System.Drawing.Point(88, 344); 
 372 
 373 this.button2.Name = "button2"; 
 374 
 375 this.button2.TabIndex = 2; 
 376 
 377 this.button2.Text = "Example2"; 
 378 
 379 this.button2.Click += new System.EventHandler(this.button2_Click); 
 380 
 381 // 
 382 
 383 // button3 
 384 
 385 // 
 386 
 387 this.button3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); 
 388 
 389 this.button3.Location = new System.Drawing.Point(176, 344); 
 390 
 391 this.button3.Name = "button3"; 
 392 
 393 this.button3.TabIndex = 3; 
 394 
 395 this.button3.Text = "WriteNode"; 
 396 
 397 this.button3.Click += new System.EventHandler(this.button3_Click); 
 398 
 399 // 
 400 
 401 // button4 
 402 
 403 // 
 404 
 405 this.button4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); 
 406 
 407 this.button4.Location = new System.Drawing.Point(264, 344); 
 408 
 409 this.button4.Name = "button4"; 
 410 
 411 this.button4.TabIndex = 4; 
 412 
 413 this.button4.Text = "Example3"; 
 414 
 415 this.button4.Click += new System.EventHandler(this.button4_Click); 
 416 
 417 // 
 418 
 419 // button5 
 420 
 421 // 
 422 
 423 this.button5.Location = new System.Drawing.Point(352, 344); 
 424 
 425 this.button5.Name = "button5"; 
 426 
 427 this.button5.TabIndex = 5; 
 428 
 429 this.button5.Text = "Example4"; 
 430 
 431 this.button5.Click += new System.EventHandler(this.button5_Click); 
 432 
 433 // 
 434 
 435 // button6 
 436 
 437 // 
 438 
 439 this.button6.Location = new System.Drawing.Point(440, 344); 
 440 
 441 this.button6.Name = "button6"; 
 442 
 443 this.button6.TabIndex = 6; 
 444 
 445 this.button6.Text = "Example5"; 
 446 
 447 this.button6.Click += new System.EventHandler(this.button6_Click); 
 448 
 449 // 
 450 
 451 // button7 
 452 
 453 // 
 454 
 455 this.button7.Location = new System.Drawing.Point(528, 344); 
 456 
 457 this.button7.Name = "button7"; 
 458 
 459 this.button7.TabIndex = 7; 
 460 
 461 this.button7.Text = "Example6"; 
 462 
 463 this.button7.Click += new System.EventHandler(this.button7_Click); 
 464 
 465 // 
 466 
 467 // Form1 
 468 
 469 // 
 470 
 471 this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); 
 472 
 473 this.ClientSize = new System.Drawing.Size(784, 373); 
 474 
 475 this.Controls.Add(this.button7); 
 476 
 477 this.Controls.Add(this.button6); 
 478 
 479 this.Controls.Add(this.button5); 
 480 
 481 this.Controls.Add(this.button4); 
 482 
 483 this.Controls.Add(this.button3); 
 484 
 485 this.Controls.Add(this.button2); 
 486 
 487 this.Controls.Add(this.button1); 
 488 
 489 this.Controls.Add(this.textBox1); 
 490 
 491 this.Name = "Form1"; 
 492 
 493 this.Text = "XMLWriting"; 
 494 
 495 this.ResumeLayout(false); 
 496 
 497 
 498 
 499 } 
 500 
 501 #endregion 
 502 
 503 
 504 
 505 /**//// <summary> 
 506 
 507 /// 应用程序的主入口点。 
 508 
 509 /// </summary> 
 510 
 511 [STAThread] 
 512 
 513 static void Main() 
 514 
 515 { 
 516 
 517 Application.Run(new Form1()); 
 518 
 519 } 
 520 
 521 
 522 
 523 private void button1_Click(object sender, System.EventArgs e) 
 524 
 525 { 
 526 
 527 this.textBox1.Text = string.Empty; 
 528 
 529 const string fileName = "WriteXml.xml"; 
 530 
 531 
 532 
 533 XmlTextWriter xmlTxtWt = new XmlTextWriter(fileName,Encoding.UTF8); 
 534 
 535 
 536 
 537 // 写XML文档声明 
 538 
 539 xmlTxtWt.WriteStartDocument(); 
 540 
 541 // 写XML起始元素 
 542 
 543 xmlTxtWt.WriteStartElement("ct","ContactDetails","http://www.deltabis.com/Contact"); 
 544 
 545 // 写文本 
 546 
 547 xmlTxtWt.WriteString("This is a XML file"); 
 548 
 549 // 写XML结束元素 
 550 
 551 xmlTxtWt.WriteEndElement(); 
 552 
 553 // 写关闭文档元素 
 554 
 555 xmlTxtWt.WriteEndDocument(); 
 556 
 557 
 558 
 559 xmlTxtWt.Flush(); //刷新 
 560 
 561 xmlTxtWt.Close(); 
 562 
 563 
 564 
 565 this.textBox1.Text = ReadXml(fileName); 
 566 
 567 } 
 568 
 569 
 570 
 571 /**//// <summary> 
 572 
 573 /// 读取经过编写的XML文件的所有内容 
 574 
 575 /// </summary> 
 576 
 577 /// <param name="xmlPath">文件路径</param> 
 578 
 579 /// <returns>表示内容的字符串</returns> 

 580 
 581 private string ReadXml(string xmlPath) 
 582 
 583 { 
 584 
 585 string xmlStr = string.Empty; 
 586 
 587 XmlTextReader xmlTxtRd = new XmlTextReader(xmlPath); 
 588 
 589 
 590 
 591 xmlTxtRd.MoveToContent(); 
 592 
 593 xmlStr = xmlTxtRd.ReadOuterXml(); 
 594 
 595 
 596 
 597 xmlTxtRd.Close(); 
 598 
 599 return xmlStr; 
 600 
 601 } 
 602 
 603 
 604 
 605 private void button2_Click(object sender, System.EventArgs e) 
 606 
 607 { 
 608 
 609 this.textBox1.Text = string.Empty; 
 610 
 611 const string fileName = "WriteXml1.xml"; 
 612 
 613 
 614 
 615 XmlTextWriter xmlTxtWt = new XmlTextWriter(fileName,Encoding.UTF8); 
 616 
 617 
 618 
 619 // 设置XML的输出格式,这里使用缩进 
 620 
 621 xmlTxtWt.Formatting = Formatting.Indented; 
 622 
 623 // 设置缩进的数量,这里是4个空格,IndentChar属性默认是空格 
 624 
 625 xmlTxtWt.Indentation = 4; 
 626 
 627 
 628 
 629 xmlTxtWt.WriteStartDocument(); 
 630 
 631 xmlTxtWt.WriteStartElement("ct","ContactDetails","http://www.deltabis.com/Contact"); 
 632 
 633 xmlTxtWt.WriteAttributeString("Date","20050121 14:00"); 
 634 
 635 xmlTxtWt.WriteElementString("contact","abcd"); 
 636 
 637 xmlTxtWt.WriteElementString("contact","efgh"); 
 638 
 639 xmlTxtWt.WriteElementString("contact","ijkl"); 
 640 
 641 xmlTxtWt.WriteElementString("contact","mnop"); 
 642 
 643 xmlTxtWt.WriteEndElement(); 
 644 
 645 xmlTxtWt.WriteEndDocument(); 
 646 
 647 
 648 
 649 xmlTxtWt.Flush(); 
 650 
 651 xmlTxtWt.Close(); 
 652 
 653 
 654 
 655 this.textBox1.Text = ReadXml(fileName); 
 656 
 657 } 
 658 
 659 
 660 
 661 // 从读取器中复制节点及其内容 
 662 
 663 private void button3_Click(object sender, System.EventArgs e) 
 664 
 665 { 
 666 
 667 XmlTextReader xmlTxtRd = new XmlTextReader("唐诗.xml"); 
 668 
 669 XmlTextWriter xmlTxtWt = new XmlTextWriter("WriteXml2.xml",Encoding.UTF8); 
 670 
 671 
 672 
 673 xmlTxtWt.Formatting = Formatting.Indented; 
 674 
 675 xmlTxtWt.Indentation = 4; 
 676 
 677 xmlTxtWt.WriteStartDocument(); 
 678 
 679 xmlTxtWt.WriteComment("以下是从读取器中拷贝的节点"); 
 680 
 681 
 682 
 683 try 
 684 
 685 { 
 686 
 687 while(xmlTxtRd.Read()) 
 688 
 689 { 
 690 
 691 if (xmlTxtRd.NodeType == XmlNodeType.Element) 
 692 
 693 xmlTxtWt.WriteNode(xmlTxtRd,true); 
 694 
 695 } 
 696 
 697 } 
 698 
 699 catch(Exception exp) 
 700 
 701 { 
 702 
 703 MessageBox.Show(exp.ToString()); 
 704 
 705 } 
 706 
 707 finally 
 708 
 709 { 
 710 
 711 xmlTxtWt.Flush(); 
 712 
 713 xmlTxtWt.Close(); 
 714 
 715 xmlTxtRd.Close(); 
 716 
 717 } 
 718 
 719 
 720 
 721 this.textBox1.Text = ReadXml("WriteXml2.xml"); 
 722 
 723 } 
 724 
 725 
 726 
 727 // 编写一份完整的XML 
 728 
 729 private void button4_Click(object sender, System.EventArgs e) 
 730 
 731 { 
 732 
 733 this.textBox1.Text = string.Empty; 
 734 
 735 string fileName = "WriteXml3.xml"; 
 736 
 737 
 738 
 739 XmlTextWriter xmlTxtWt = new XmlTextWriter(fileName,Encoding.UTF8); 
 740 
 741 
 742 
 743 xmlTxtWt.Formatting = Formatting.Indented; 
 744 
 745 xmlTxtWt.Indentation = 4; 
 746 
 747 
 748 
 749 xmlTxtWt.WriteStartDocument(); 
 750 
 751 xmlTxtWt.WriteStartElement("ct","ContactDetails","http://www.deltabis.com/Contact"); 
 752 
 753 xmlTxtWt.WriteAttributeString("Date","20050121 16:00"); 
 754 
 755 xmlTxtWt.WriteComment("This document contains contact information."); 
 756 
 757 xmlTxtWt.WriteStartElement("contact"); 
 758 
 759 xmlTxtWt.WriteAttributeString("title",string.Empty); 
 760 
 761 xmlTxtWt.WriteStartElement("name"); 
 762 
 763 xmlTxtWt.WriteElementString("firstname","Steven"); 
 764 
 765 xmlTxtWt.WriteElementString("middle",string.Empty); 
 766 
 767 xmlTxtWt.WriteElementString("lastname","LivingStone-Perez"); 
 768 
 769 xmlTxtWt.WriteFullEndElement(); 
 770 
 771 xmlTxtWt.WriteFullEndElement(); 
 772 
 773 xmlTxtWt.WriteFullEndElement(); 
 774 
 775 xmlTxtWt.WriteEndDocument(); 
 776 
 777 
 778 
 779 xmlTxtWt.Flush(); 
 780 
 781 xmlTxtWt.Close(); 
 782 
 783 
 784 
 785 this.textBox1.Text = ReadXml(fileName); 
 786 
 787 } 
 788 
 789 
 790 
 791 // 添加CDATA数据块 
 792 
 793 private void button5_Click(object sender, System.EventArgs e) 
 794 
 795 { 
 796 
 797 this.textBox1.Text = string.Empty; 
 798 
 799 string fileName = "WriteXml4.xml"; 
 800 
 801 
 802 
 803 XmlTextWriter xmlTxtWt = new XmlTextWriter(fileName,Encoding.UTF8); 
 804 
 805 
 806 
 807 xmlTxtWt.Formatting = Formatting.Indented; 
 808 
 809 xmlTxtWt.Indentation = 4; 
 810 
 811 
 812 
 813 xmlTxtWt.WriteStartDocument(); 
 814 
 815 xmlTxtWt.WriteStartElement("ct","ContactDetails","http://www.deltabis.com/Contact"); 
 816 
 817 xmlTxtWt.WriteAttributeString("Date","20050121 16:00"); 
 818 
 819 xmlTxtWt.WriteComment("This document contains contact information."); 
 820 
 821 xmlTxtWt.WriteStartElement("contact"); 
 822 
 823 xmlTxtWt.WriteAttributeString("title",string.Empty); 
 824 
 825 xmlTxtWt.WriteStartElement("name"); 
 826 
 827 xmlTxtWt.WriteElementString("firstname","Steven"); 
 828 
 829 xmlTxtWt.WriteElementString("middle",string.Empty); 
 830 
 831 xmlTxtWt.WriteElementString("lastname","LivingStone-Perez"); 
 832 
 833 xmlTxtWt.WriteFullEndElement(); 
 834 
 835 xmlTxtWt.WriteStartElement("notes","http://www.deltabis.com/Contact"); // 该节点的命名空间与上面一样,该节点将使用上面的前缀 
 836 
 837 xmlTxtWt.WriteCData("<securityAlogrithm>88hshshhhdd8*^&@^*^#*&!%~~~(ghj*(**&%^){}^(*&7*(9$%###$@!"); 
 838 
 839 xmlTxtWt.WriteEndElement(); 
 840 
 841 xmlTxtWt.WriteFullEndElement(); 
 842 
 843 xmlTxtWt.WriteFullEndElement(); 
 844 
 845 xmlTxtWt.WriteEndDocument(); 
 846 
 847 
 848 
 849 xmlTxtWt.Flush(); 
 850 
 851 xmlTxtWt.Close(); 
 852 
 853 
 854 
 855 this.textBox1.Text = ReadXml(fileName); 
 856 
 857 } 
 858 
 859 
 860 
 861 // 对图片进行编码,并写出 
 862 
 863 private void button6_Click(object sender, System.EventArgs e) 
 864 
 865 { 
 866 
 867 int readByte = 0; 
 868 
 869 int bytesToRead = 100; 
 870 
 871 string fileName = "WriteXml5.xml"; 
 872 
 873 this.textBox1.Text = string.Empty; 
 874 
 875 
 876 
 877 // 打开图片文件,利用该图片构造一个文件流 
 878 
 879 FileStream fs = new FileStream(@"D:\03.jpg",FileMode.Open); 
 880 
 881 // 使用文件流构造一个二进制读取器将基元数据读作二进制值 
 882 
 883 BinaryReader br = new BinaryReader(fs); 
 884 
 885 
 886 
 887 XmlTextWriter xmlTxtWt = new XmlTextWriter(fileName,Encoding.UTF8); 
 888 
 889 xmlTxtWt.Formatting = Formatting.Indented; 
 890 
 891 xmlTxtWt.Indentation = 4; 
 892 
 893 
 894 
 895 xmlTxtWt.WriteStartDocument(); 
 896 
 897 xmlTxtWt.WriteStartElement("ct","ContactDetails","http://www.deltabis.com/Contact"); 
 898 
 899 xmlTxtWt.WriteStartElement("image"); 
 900 
 901 xmlTxtWt.WriteAttributeString("imageName","03.jpg"); 
 902 
 903 
 904 
 905 byte[] base64buffer = new byte[bytesToRead]; 
 906 
 907 do 
 908 
 909 { 
 910 
 911 readByte = br.Read(base64buffer,0,bytesToRead); //将数据读入字节数组 
 912 
 913 xmlTxtWt.WriteBase64(base64buffer,0,readByte); //将数组中二进制值编码为Base64并写出到XML文件 
 914 
 915 }while(bytesToRead <= readByte); 
 916 
 917 
 918 
 919 xmlTxtWt.WriteEndElement(); 
 920 
 921 xmlTxtWt.WriteEndElement(); 
 922 
 923 xmlTxtWt.WriteEndDocument(); 
 924 
 925 
 926 
 927 xmlTxtWt.Flush(); 
 928 
 929 xmlTxtWt.Close(); 
 930 
 931 
 932 
 933 this.textBox1.Text = ReadXml(fileName); 
 934 
 935 } 
 936 
 937 
 938 
 939 // 解码并生成图片 
 940 
 941 private void button7_Click(object sender, System.EventArgs e) 
 942 
 943 { 
 944 
 945 int readByte = 0; 
 946 
 947 int bytesToRead = 100; 
 948 
 949 
 950 
 951 XmlTextReader xmlTxtRd = new XmlTextReader("WriteXml5.xml"); 
 952 
 953 
 954 
 955 FileStream fs = new FileStream("newimage.jpg",FileMode.Create); 
 956 
 957 BinaryWriter bw = new BinaryWriter(fs); 
 958 
 959 
 960 
 961 byte[] base64buffer = new byte[bytesToRead]; 
 962 
 963 
 964 
 965 while(xmlTxtRd.Read()) 
 966 
 967 { 
 968 
 969 if (xmlTxtRd.NodeType == XmlNodeType.Element && xmlTxtRd.Name == "image") 
 970 
 971 { 
 972 
 973 do 
 974 
 975 { 
 976 
 977 readByte = xmlTxtRd.ReadBase64(base64buffer,0,bytesToRead); 
 978 
 979 bw.Write(base64buffer,0,readByte); 
 980 
 981 }while(readByte <= bytesToRead); 
 982 
 983 } 
 984 
 985 } 
 986 
 987 
 988 
 989 bw.Flush(); 
 990 
 991 bw.Close(); 
 992 
 993 fs.Close(); 
 994 
 995 
 996 
 997 xmlTxtRd.Close(); 
 998 
 999 } 
1000 
1001 } 
1002 
1003 } 
原文地址:https://www.cnblogs.com/zgblog/p/2539124.html