好学IT学院:IT信息技术分享交流平台
标签:C#  来源:中国站长学院  作者:佚名  发布时间:2009-07-06  ★★★加入收藏〗〖手机版
摘要:对于XML,想必各位都比较了解,我也就不用费笔墨来描述它是什么了,我想在未来的Web开发中XML一定会大放异彩,XML是可扩展标记语言,使用它企业可以制定一套自己的数据格式,数据按照这种格式在网络中传输然后再通过XSLT将数据转换成用户期望的样子表示出来,这样…

#region XmlReader 的属性
  /// <summary>
  /// 获取或设置列表框用于显示xml
  /// </summary>
  public ListBox listBox
  {
   get
   {
    return this._listBox;
   }
   set
   {
    this._listBox = value;
   }
  }

/// <summary>
  /// 获取或设置xml文件的绝对路径
  /// </summary>
  public string xmlPath
  {
   get
   {
    return this._xmlPath;
   }
   set
   {
    this._xmlPath = value;
   }
  }
  #endregion

/// <summary>
  /// 遍历Xml文件
  /// </summary>
  public void EachXml()
  {
   this._listBox.Items.Clear();
   this.xmlTxtRd = new XmlTextReader(this._xmlPath);

 try
   {
    while(xmlTxtRd.Read())
    {
     this._listBox.Items.Add(this.xmlTxtRd.Value);
    }
   }
   catch(XmlException exp)
   {
    throw new XmlException(_errMsg + this._xmlPath + exp.ToString());
   }
   finally
   {
    if (this.xmlTxtRd != null)
     this.xmlTxtRd.Close();
   }
  }

/// <summary>
  /// 读取Xml文件的节点类型
  /// </summary>
  public void ReadXmlByNodeType()
  {
   this._listBox.Items.Clear();
   this.xmlTxtRd = new XmlTextReader(this._xmlPath);

 try
   {
    while(xmlTxtRd.Read())
    {
     this._listBox.Items.Add(this.xmlTxtRd.NodeType.ToString());
    }
   }
   catch(XmlException exp)
   {
    throw new XmlException(_errMsg + this._xmlPath + exp.ToString());
   }
   finally
   {
    if (this.xmlTxtRd != null)
     this.xmlTxtRd.Close();
   }
  }

/// <summary>
  /// 根据节点类型过滤Xml文档
  /// </summary>
  /// <param name="xmlNType">XmlNodeType 节点类型的数组</param>
  public void FilterByNodeType(XmlNodeType[] xmlNType)
  {
   this._listBox.Items.Clear();
   this.xmlTxtRd = new XmlTextReader(this._xmlPath);
   try
   {
    while(xmlTxtRd.Read())
    {
     for (int i = 0; i < xmlNType.Length; i++)
     {
      if (xmlTxtRd.NodeType == xmlNType[i])
      {
       this._listBox.Items.Add(xmlTxtRd.Name + " is Type " + xmlTxtRd.NodeType.ToString());
      }
     }
    }
   }
   catch(XmlException exp)
   {
    throw new XmlException(_errMsg + this.xmlPath + exp.ToString());
   }
   finally
   {
    if (this.xmlTxtRd != null)
     this.xmlTxtRd.Close();
   }
  }

/// <summary>
  /// 读取Xml文件的所有文本节点值
  /// </summary>
  public void ReadXmlTextValue()
  {
   this._listBox.Items.Clear();
   this.xmlTxtRd = new XmlTextReader(this._xmlPath);

 try
   {
    while(xmlTxtRd.Read())
    {
     if (xmlTxtRd.NodeType == XmlNodeType.Text)
     {
      this._listBox.Items.Add(xmlTxtRd.Value);
     }
    }
   }
   catch(XmlException xmlExp)
   {
    throw new XmlException(_errMsg + this._xmlPath + xmlExp.ToString());
   }
   finally
   {
    if (this.xmlTxtRd != null)
     this.xmlTxtRd.Close();
   }
  }

/// <summary>
  /// 读取Xml文件的属性
  /// </summary>
  public void ReadXmlAttributes()
  {
   this._listBox.Items.Clear();
   this.xmlTxtRd = new XmlTextReader(this._xmlPath);

 try
   {
    while(xmlTxtRd.Read())
    {
     if (xmlTxtRd.NodeType == XmlNodeType.Element)
     {
      if (xmlTxtRd.HasAttributes)
      {
       this._listBox.Items.Add("The Element " + xmlTxtRd.Name + " has " + xmlTxtRd.AttributeCount + " Attributes");

 this._listBox.Items.Add("The Attributes are:");

 while(xmlTxtRd.MoveToNextAttribute())
       {
        this._listBox.Items.Add(xmlTxtRd.Name + " = " + xmlTxtRd.Value);
       }
      }
      else
      {
       this._listBox.Items.Add("The Element " + xmlTxtRd.Name + " has no Attribute");
      }
      this._listBox.Items.Add("");
     }
    }
   }
   catch(XmlException xmlExp)
   {
    throw new XmlException(_errMsg + this._xmlPath + xmlExp.ToString());
   }
   finally
   {
    if (this.xmlTxtRd != null)
     this.xmlTxtRd.Close();
   }
  }

  • 好学考试H5触屏版开放内测
  • 好学触屏公众号虎力全开、杨帆起航!