大话人生

  IT博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  299 随笔 :: 0 文章 :: 73 评论 :: 0 Trackbacks

XML 学习
1 读写XML
2 使用文档对象建模(DOM)
3 对数据集使用XML
4 使用C# 编辑XML文件
可扩展标记语言已经(XML)已经成为标记共享数据的标准。
XML文件可以用于很多方面,包括标记标记的简单数据文件和存储临时数据的简单任务,
以及将信息从一个程序和进程传递到另一个数据和进程的复杂任务。
System.Xml 包括:System.Xml.Xpath     System.Xml.Schema     System.Xml.Xsl
程序中使用那个命名空间将取决于要实现的功能。
例如:
    想使用部分XML文档,并操作字符串和数字。 包含System.Xml.XPath
    执行构架映射和验证,System,,Xml.Schema
    能将 XML数据转换成Html或者其他格式    System.Xml.Xsl
    这些命名空间包括4类:
    (1) 执行基本的XML分析和编写操作的类
    (2) 在文档,结点或者结构中编辑XML的类。
    (3) 验证XML的类
    (4) 转换XML的类
 1 读写XML
     (1) 使用XMLTextWrite创建XML
         XMLTextWrite 创建XMLTextWrite 实例
         重载列表
         [C#] public XmlTextWriter(TextWriter);
         public XmlTextWriter(Stream, Encoding);
  public XmlTextWriter(string, Encoding);
         Encoding属性包括:System.Text..ASCIIEncoding,
  System.Text.UnicodeEncoding, System.Text.UTF7Encoding, System.Text.UTF8Encoding(包含在System.Text里面)
         例如需要 在 C:\XmlData 目录下面创建一个myTextWrite.xml文件,可以使用下面的构造函数
  XmlTextWriter myWriter=new XmlTextWriter("C:\XmlData\myTextWrite.xml",null)
         Xml文件包括:Xml文件声明,注释,元素,子元素,属性
  1 创建XML声明:     myWriter.WriteStartDocument();  //对这个方法调用打开一个新的XML文档,并在XML文件和流中
                                 写入一下文本:<?xml version="1.0" encoding="utf--8"?>
  2 添加注释。尽管不是必要在Xml中添加注释,但是这样做最好.注释:<!--...-->
                         [C#]
                                public override void WriteComment(string text);
    例如:myWriter.WriterComment("comment text");//调用这个方法产生<!--comment text-->
                                WriteComment方法使用合适的XML标记编排注释格式
 3 编写元素和子元素  XML文件最重要的部分是包含元素的区域,主要数据都存储在元素中.
   有2种技术用于创建元素,可以同时编写元素和他的值,或者打开元素,写入元素的内容,然后关闭元素.
   (1)利用WriteElementString方法我们能同时编写元素和他的值.
   XmlWriter.WriteElementString 方法      当在派生类中被重写时,写出包含字符串值的元素。
   格式:
   public void WriteElementString(string, string);  
   这里第一个参数是要创建元素的元素名称, 第二个参数是元素的值
   例如: myWriter.WriteElementString("Video","Gone with the wind");//产生的XML是<Video>Gone With the wind</Video>
   这个方法编写了Video元素的开始标记符,内容和结束标记符。
   如果想创建包含子元素的元素,或者想在一个元素里面添加属性,那么必须打开元素,编写标识符和其他一切
   数据.调用不带参数的WriteEndElement方法,创建结束标识符,关闭这个元素.
   例如:
   myWriter.WriteStartElement("Video");
   myWriter.WriteString("Gone with thw wind");
   myWriter.WriteEndElement();

   public void WriteElementString(string, string, string);
   (2)添加属性
   XmlTextWriter类还可以通过WriteAttributeString方法创建属性.
   XmlTextWriter 中的属性命名空间前缀(来自msdn)
   有多种方法可以处理 XmlTextWriter 的属性 WriteAttributes、WriteAttributeString 和 WriteStartAttribute 的命名空间前缀。
          WriteAttributes
           如果当前位置是元素节点,则 WriteAttributes 方法写出在 XmlReader 中的当前位置找到的所有属性。如果 XmlReader 声明了命名空间前缀,则除属性之外 WriteAttributes 方法还写出该命名空间前缀。下面的代码示例显示 WriteAttributes 方法如何写出属性的命名空间前缀。
    例如:myWriter.WriteStartElement("Length");
                myWriter.WriteAttributeString("Measurement","Minutes");
         myWriter.WriteString("120");
         myWriter.WriteEndElement();
         //这段代码产生<Length Measurement="Minutes">120</Length>
         更高级的办法是WriteStartAttribute和WriteEndAttribute方法进行更多的属性定义。
           (3)XML文件中添加格式和空格
    XML不需要任何特别的空格或者缩进,但是添加格式和空格可以使XML文件查看起来更容易。
    创建XML文件的时候,可能需要 设置XMLTextWriter对象的以下4个属性
    Formatting     Indentation     IndentChar     QuoteChar
           XmlTextWriter.Formatting 属性         指示如何对输出进行格式设置。public Formatting Formatting {get; set;}
           属性值   Formatting 值之一。默认值为 Formatting.None(不进行特殊的格式设置)。
    备注
           如果设置了“缩进”选项,则使用 Indentation 和 IndentChar 属性对子元素进行缩进。只缩进元素内容。下面的 C# 代码写出包括混合内容的 HTML 元素:
           例如:
    XmlTextWriter w = new XmlTextWriter(Console.Out);
           w.Formatting = Formatting.Indented;
           w.WriteStartElement("ol");
           w.WriteStartElement("li");
           w.WriteString("The big "); // This means "li" now has a mixed content model.
           w.WriteElementString("b", "E");
           w.WriteElementString("i", "lephant");
           w.WriteString(" walks slowly.");
           w.WriteEndElement();
           w.WriteEndElement();
    上面的代码产生下面的输出:
           <ol>
            <li>The big <b>E</b><i>lephant</i> walks slowly.</li>
           </ol>
           (4) 关闭XML文件或者流
    myWriter.WriteEndDocument();
    myWriter.Close();

使用XmlTextWriter的应用程序示例()

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using System.Text;


namespace XMLApp
{
    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>

    public class Form1 : System.Windows.Forms.Form
    
{
        
private System.Windows.Forms.Label label1;
        
private System.Windows.Forms.Label label2;
        
private System.Windows.Forms.Label label3;
        
private System.Windows.Forms.Label label4;
        
private System.Windows.Forms.TextBox textBox1;
        
private System.Windows.Forms.TextBox textBox2;
        
private System.Windows.Forms.TextBox textBox3;
        
private System.Windows.Forms.ComboBox comboBox1;
        
private System.Windows.Forms.Button button1;
        
private System.Windows.Forms.Button button2;
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.Container components = null;
        
private System.Xml.XmlTextWriter xtw;

        
public Form1()
        
{
            
//
            
// Windows 窗体设计器支持所必需的
            
//
            InitializeComponent();

            xtw
=new XmlTextWriter("temp.xml",null);
            xtw.Formatting
=Formatting.Indented;
            xtw.Indentation
=3;

            xtw.WriteStartDocument();
            xtw.WriteComment(
"Video Library"); 
            xtw.WriteComment(
"This is a file containing a number of videos");
            xtw.WriteStartElement(
"VideoLibrary"); 

            
//
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
        }


        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows 窗体设计器生成的代码

        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new Form1());
        }


        
private void Form1_Load(object sender, System.EventArgs e)
        
{
            comboBox1.Items.Clear();            
            comboBox1.Items.Add(
"A1"); 
            comboBox1.Items.Add(
"A2");
            comboBox1.Items.Add(
"A3");
            comboBox1.Items.Add(
"A4");
            comboBox1.SelectedIndex
=0;    
        }

        
private void Quit(object sender,System.EventArgs e)
        
{
            xtw.WriteEndElement();
            xtw.WriteEndDocument();
            xtw.Flush();
            xtw.Close();
            Application.Exit();
        }

        
private void button2_Click(object sender, System.EventArgs e)
        
{
            Quit(
null,EventArgs.Empty); 
        }


        
private void button1_Click(object sender, System.EventArgs e)
        
{
            xtw.WriteStartElement(
"Video");
            xtw.WriteElementString(
"TiTle",textBox1.Text);
            

            xtw.WriteElementString(
"Star",textBox3.Text);
            xtw.WriteElementString(
"rating",comboBox1.SelectedText);
            
            xtw.WriteEndElement();
            
            textBox1.Focus();

            textBox1.Text
="";
            textBox2.Text
="";
            textBox3.Text
="";

        }

    }

}

发表于 @ 2005年12月26日 11:22:00|评论(0 )|编辑

posted on 2008-05-16 02:09 大话人生 阅读(438) 评论(0)  编辑 收藏 引用 所属分类: 开发
只有注册用户登录后才能发表评论。