C#中使用XML——读取XML

xml

不对于XML,想必各位都比较了解,我也就不用费笔墨来描述它是什么了,我想在未来的Web开发中XML一定会大放异彩,XML是可扩展标记语言,使用它企业可以制定一套自己的数据格式,数据按照这种格式在网络中传输然后再通过XSLT将数据转换成用户期望的样子表示出来,这样便轻易的解决了数据格式不兼容的问题。用于Internet的数据传输,我想,这是XML对于我们这些程序员最诱人的地方!

我们今天的主题不是论述XML的好处,而是讨论在C#中如何使用XML。下面我们来了解一下使用程序访问XML的一些基础理论知识。

访问的两种模型:

在程序中访问进而操作XML文件一般有两种模型,分别是使用DOM(文档对象模型)和流模型,使用DOM的好处在于它允许编辑和更新XML文档,可以随机访问文档中的数据,可以使用XPath查询,但是,DOM的缺点在于它需要一次性的加载整个文档到内存中,对于大型的文档,这会造成资源问题。流模型很好的解决了这个问题,因为它对XML文件的访问采用的是流的概念,也就是说,任何时候在内存中只有当前节点,但它也有它的不足,它是只读的,仅向前的,不能在文档中执行向后导航操作。虽然是各有千秋,但我们也可以在程序中两者并用实现优劣互补嘛,呵呵,这是题外话了!我们今天主要讨论XML的读取,那我们就详细讨论一下流模型吧!

流模型中的变体:

流模型每次迭代XML文档中的一个节点,适合于处理较大的文档,所耗内存空间小。流模型中有两种变体——“推”模型和“拉”模型。

推模型也就是常说的SAX,SAX是一种靠事件驱动的模型,也就是说:它每发现一个节点就用推模型引发一个事件,而我们必须编写这些事件的处理程序,这样的做法非常的不灵活,也很麻烦。

.NET中使用的是基于“拉”模型的实现方案,“拉”模型在遍历文档时会把感兴趣的文档部分从读取器中拉出,不需要引发事件,允许我们以编程的方式访问文档,这大大的提高了灵活性,在性能上“拉”模型可以选择性的处理节点,而SAX每发现一个节点都会通知客户机,从而,使用“拉”模型可以提高Application的整体效率。在.NET中“拉”模型是作为XmlReader类实现的,下面看一下该类的继承结构:

我们今天来讲一下该体系结构中的XmlTextReader类,该类提供对Xml文件进行读取的功能,它可以验证文档是否格式良好,如果不是格式良好的Xml文档,该类在读取过程中将会抛出XmlException异常,可使用该类提供的一些方法对文档节点进行读取,筛选等操作以及得到节点的名称和值,请牢记:XmlTextReader是基于流模型的实现,打个不恰当的比喻,XML文件就好象水源,闸一开水就流出,流过了就流过了不会也不可以往回流。内存中任何时候只有当前节点,你可以使用XmlTextReader类的Read()方法读取下一个节点。好了,说了这么多来看一个例子,编程要注重实际对吧。看代码前先看下运行效果吧!

Example1按纽遍历文档读取数据,Example2,Example3按纽得到节点类型,Example4过滤文档只获得数据内容,Example5得到属性节点,Example6按纽得到命名空间,Example7显示整个XML文档,为此,我专门写一个类来封装以上功能,该类代码如下:

//---------------------------------------------------------------------------------------------------

//XmlReader类用于Xml文件的一般读取操作,以下对这个类做简单介绍:

//

//Attributes(属性):

//listBox:设置该属性主要为了得到客户端控件以便于显示所读到的文件的内容(这里是ListBox控件)

//xmlPath:设置该属性为了得到一个确定的Xml文件的绝对路径

//

//BasilicUsing(重要的引用):

//System.Xml:该命名空间中封装有对Xml进行操作的常用类,本类中使用了其中的XmlTextReader类

//XmlTextReader:该类提供对Xml文件进行读取的功能,它可以验证文档是否格式良好,如果不是格式

//良好的Xml文档,该类在读取过程中将会抛出XmlException异常,可使用该类提供的

//一些方法对文档节点进行读取,筛选等操作以及得到节点的名称和值

//

//boolXmlTextReader.Read():读取流中下一个节点,当读完最后一个节点再次调用该方法该方法返回false

//XmlNodeTypeXmlTextReader.NodeType:该属性返回当前节点的类型

//XmlNodeType.Element元素节点

//XmlNodeType.EndElement结尾元素节点

//XmlNodeType.XmlDeclaration文档的第一个节点

//XmlNodeType.Text文本节点

//boolXmlTextReader.HasAttributes:当前节点有没有属性,返回true或false

//stringXmlTextReader.Name:返回当前节点的名称

//stringXmlTextReader.value:返回当前节点的值

//stringXmlTextReader.LocalName:返回当前节点的本地名称

//stringXmlTextReader.NamespaceURI:返回当前节点的命名空间URI

//stringXmlTextReader.Prefix:返回当前节点的前缀

//boolXmlTextReader.MoveToNextAttribute():移动到当前节点的下一个属性

//---------------------------------------------------------------------------------------------------

namespaceXMLReading

{

usingSystem;

usingSystem.Xml;

usingSystem.Windows.Forms;

usingSystem.ComponentModel;

///

///Xml文件读取器

///

publicclassXmlReader:IDisposable

{

privatestring_xmlPath;

privateconststring_errMsg="ErrorOccurredWhileReading";

privateListBox_listBox;

privateXmlTextReaderxmlTxtRd;

#regionXmlReader的构造器

publicXmlReader()

{

this._xmlPath=string.Empty;

this._listBox=null;

this.xmlTxtRd=null;

}

///

///构造器

///

///xml文件绝对路径

///列表框用于显示xml

publicXmlReader(string_xmlPath,ListBox_listBox)

{

this._xmlPath=_xmlPath;

this._listBox=_listBox;

this.xmlTxtRd=null;

}

#endregion

#regionXmlReader的资源释放方法

///

///清理该对象所有正在使用的资源

///

publicvoidDispose()

{

this.Dispose(true);

GC.SuppressFinalize(this);

}

///

///释放该对象的实例变量

///

///

protectedvirtualvoidDispose(booldisposing)

{

if(!disposing)

return;

if(this.xmlTxtRd!=null)

{

this.xmlTxtRd.Close();

this.xmlTxtRd=null;

}

if(this._xmlPath!=null)

{

this._xmlPath=null;

}

}

#endregion

#regionXmlReader的属性

///

///获取或设置列表框用于显示xml

///

publicListBoxlistBox

{

get

{

returnthis._listBox;

}

set

{

this._listBox=value;

}

}

///

///获取或设置xml文件的绝对路径

///

publicstringxmlPath

{

get

{

returnthis._xmlPath;

}

set

{

this._xmlPath=value;

}

}

#endregion

///

///遍历Xml文件

///

publicvoidEachXml()

{

this._listBox.Items.Clear();

this.xmlTxtRd=newXmlTextReader(this._xmlPath);

try

{

while(xmlTxtRd.Read())

{

this._listBox.Items.Add(this.xmlTxtRd.value);

}

}

catch(XmlExceptionexp)

{

thrownewXmlException(_errMsg+this._xmlPath+exp.ToString());

}

finally

{

if(this.xmlTxtRd!=null)

this.xmlTxtRd.Close();

}

}

///

///读取Xml文件的节点类型

///

publicvoidReadXmlByNodeType()

{

this._listBox.Items.Clear();

this.xmlTxtRd=newXmlTextReader(this._xmlPath);

try

{

while(xmlTxtRd.Read())

{

this._listBox.Items.Add(this.xmlTxtRd.NodeType.ToString());

}

}

catch(XmlExceptionexp)

{

thrownewXmlException(_errMsg+this._xmlPath+exp.ToString());

}

finally

{

if(this.xmlTxtRd!=null)

this.xmlTxtRd.Close();

}

}

///

///根据节点类型过滤Xml文档

///

///XmlNodeType节点类型的数组

publicvoidFilterByNodeType(XmlNodeType[]xmlNType)

{

this._listBox.Items.Clear();

this.xmlTxtRd=newXmlTextReader(this._xmlPath);

try

{

while(xmlTxtRd.Read())

{

for(inti=0;i

{

if(xmlTxtRd.NodeType==xmlNType[i])

{

this._listBox.Items.Add(xmlTxtRd.Name+"isType"+xmlTxtRd.NodeType.ToString());

}

}

}

}

catch(XmlExceptionexp)

{

thrownewXmlException(_errMsg+this.xmlPath+exp.ToString());

}

finally

{

if(this.xmlTxtRd!=null)

this.xmlTxtRd.Close();

}

}

///

///读取Xml文件的所有文本节点值

///

publicvoidReadXmlTextvalue()

{

this._listBox.Items.Clear();

this.xmlTxtRd=newXmlTextReader(this._xmlPath);

try

{

while(xmlTxtRd.Read())

{

if(xmlTxtRd.NodeType==XmlNodeType.Text)

{

this._listBox.Items.Add(xmlTxtRd.value);

}

}

}

catch(XmlExceptionxmlExp)

{

thrownewXmlException(_errMsg+this._xmlPath+xmlExp.ToString());

}

finally

{

if(this.xmlTxtRd!=null)

this.xmlTxtRd.Close();

}

}

///

///读取Xml文件的属性

///

publicvoidReadXmlAttributes()

{

this._listBox.Items.Clear();

this.xmlTxtRd=newXmlTextReader(this._xmlPath);

try

{

while(xmlTxtRd.Read())

{

if(xmlTxtRd.NodeType==XmlNodeType.Element)

{

if(xmlTxtRd.HasAttributes)

{

this._listBox.Items.Add("TheElement"+xmlTxtRd.Name+"has"+xmlTxtRd.AttributeCount+"Attributes");

this._listBox.Items.Add("TheAttributesare:");

while(xmlTxtRd.MoveToNextAttribute())

{

this._listBox.Items.Add(xmlTxtRd.Name+"="+xmlTxtRd.value);

}

}

else

{

this._listBox.Items.Add("TheElement"+xmlTxtRd.Name+"hasnoAttribute");

}

this._listBox.Items.Add("");

}

}

}

catch(XmlExceptionxmlExp)

{

thrownewXmlException(_errMsg+this._xmlPath+xmlExp.ToString());

}

finally

{

if(this.xmlTxtRd!=null)

this.xmlTxtRd.Close();

}

}

///

///读取Xml文件的命名空间

///

publicvoidReadXmlNamespace()

{

this._listBox.Items.Clear();

this.xmlTxtRd=newXmlTextReader(this._xmlPath);

try

{

while(xmlTxtRd.Read())

{

if(xmlTxtRd.NodeType==XmlNodeType.Element&&xmlTxtRd.Prefix!="")

{

this._listBox.Items.Add("ThePrefix"+xmlTxtRd.Prefix+"isassociatedwithnamespace"+xmlTxtRd.NamespaceURI);

this._listBox.Items.Add("TheElementwiththelocalname"+xmlTxtRd.LocalName+"isassociatedwith"+"thenamespace"+xmlTxtRd.NamespaceURI);

}

if(xmlTxtRd.NodeType==XmlNodeType.Element&&xmlTxtRd.HasAttributes)

{

while(xmlTxtRd.MoveToNextAttribute())

{

if(xmlTxtRd.Prefix!="")

{

this._listBox.Items.Add("ThePrefix"+xmlTxtRd.Prefix+"isassociatedwithnamespace"+xmlTxtRd.NamespaceURI);

this._listBox.Items.Add("TheAttributewiththelocalname"+xmlTxtRd.LocalName+"isassociatedwiththenamespace"+xmlTxtRd.NamespaceURI);

}

}

}

}

}

catch(XmlExceptionxmlExp)

{

thrownewXmlException(_errMsg+this._xmlPath+xmlExp.ToString());

}

finally

{

if(this.xmlTxtRd!=null)

this.xmlTxtRd.Close();

}

}

///

///读取整个Xml文件

///

publicvoidReadXml()

{

stringattAndEle=string.Empty;

this._listBox.Items.Clear();

this.xmlTxtRd=newXmlTextReader(this._xmlPath);

try

{

while(xmlTxtRd.Read())

{

if(xmlTxtRd.NodeType==XmlNodeType.XmlDeclaration)

this._listBox.Items.Add(string.Format("<?{0}{1}?>",xmlTxtRd.Name,xmlTxtRd.value));

elseif(xmlTxtRd.NodeType==XmlNodeType.Element)

{

attAndEle=string.Format("<{0}",xmlTxtRd.Name);

if(xmlTxtRd.HasAttributes)

{

while(xmlTxtRd.MoveToNextAttribute())

{

attAndEle=attAndEle+string.Format("{0}='{1}'",xmlTxtRd.Name,xmlTxtRd.value);

}

}

attAndEle=attAndEle.Trim()+">";

this._listBox.Items.Add(attAndEle);

}

elseif(xmlTxtRd.NodeType==XmlNodeType.EndElement)

this._listBox.Items.Add(string.Format("{0}>",xmlTxtRd.Name));

elseif(xmlTxtRd.NodeType==XmlNodeType.Text)

this._listBox.Items.Add(xmlTxtRd.value);

}

}

catch(XmlExceptionxmlExp)

{

thrownewXmlException(_errMsg+this._xmlPath+xmlExp.ToString());

}

finally

{

if(this.xmlTxtRd!=null)

this.xmlTxtRd.Close();

}

}

}

}

窗体代码如下:

namespaceXMLReading

{

usingSystem;

usingSystem.Drawing;

usingSystem.Collections;

usingSystem.ComponentModel;

usingSystem.Windows.Forms;

usingSystem.Data;

usingSystem.Xml;

publicclassForm1:System.Windows.Forms.Form

{

privateSystem.Windows.Forms.ListBoxlistBox1;

privateSystem.Windows.Forms.Buttonbutton1;

privateSystem.Windows.Forms.Buttonbutton2;

privateSystem.Windows.Forms.Buttonbutton3;

privateSystem.Windows.Forms.Buttonbutton4;

privateSystem.Windows.Forms.Buttonbutton5;

privateSystem.Windows.Forms.Buttonbutton6;

privateSystem.Windows.Forms.Buttonbutton7;

privatestringxmlPath;

privateXmlReaderxRead;

///

///必需的设计器变量。

///

privateSystem.ComponentModel.Containercomponents=null;

publicForm1()

{

InitializeComponent();

}

///

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

///

protectedoverridevoidDispose(booldisposing)

{

if(disposing)

{

if(components!=null)

{

components.Dispose();

}

}

base.Dispose(disposing);

}

#regionWindows窗体设计器生成的代码

///

///设计器支持所需的方法-不要使用代码编辑器修改

///此方法的内容。

///

privatevoidInitializeComponent()

{

this.listBox1=newSystem.Windows.Forms.ListBox();

this.button1=newSystem.Windows.Forms.Button();

this.button2=newSystem.Windows.Forms.Button();

this.button3=newSystem.Windows.Forms.Button();

this.button4=newSystem.Windows.Forms.Button();

this.button5=newSystem.Windows.Forms.Button();

this.button6=newSystem.Windows.Forms.Button();

this.button7=newSystem.Windows.Forms.Button();

this.SuspendLayout();

//

//listBox1

//

this.listBox1.Anchor=((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top|System.Windows.Forms.AnchorStyles.Bottom)

|System.Windows.Forms.AnchorStyles.Left)

|System.Windows.Forms.AnchorStyles.Right)));

this.listBox1.ItemHeight=12;

this.listBox1.Location=newSystem.Drawing.Point(8,8);

this.listBox1.Name="listBox1";

this.listBox1.Size=newSystem.Drawing.Size(716,460);

this.listBox1.TabIndex=0;

//

//button1

//

this.button1.Anchor=((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom|System.Windows.Forms.AnchorStyles.Left)));

this.button1.Location=newSystem.Drawing.Point(8,488);

this.button1.Name="button1";

this.button1.TabIndex=1;

this.button1.Text="Example1";

this.button1.Click+=newSystem.EventHandler(this.button1_Click);

//

//button2

//

this.button2.Anchor=((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom|System.Windows.Forms.AnchorStyles.Left)));

this.button2.Location=newSystem.Drawing.Point(96,488);

this.button2.Name="button2";

this.button2.TabIndex=2;

this.button2.Text="Example2";

this.button2.Click+=newSystem.EventHandler(this.button2_Click);

//

//button3

//

this.button3.Anchor=((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom|System.Windows.Forms.AnchorStyles.Right)));

this.button3.Location=newSystem.Drawing.Point(648,488);

this.button3.Name="button3";

this.button3.TabIndex=3;

this.button3.Text="Example7";

this.button3.Click+=newSystem.EventHandler(this.button3_Click);

//

//button4

//

this.button4.Anchor=((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom|System.Windows.Forms.AnchorStyles.Left)));

this.button4.Location=newSystem.Drawing.Point(184,488);

this.button4.Name="button4";

this.button4.TabIndex=4;

this.button4.Text="Example3";

this.button4.Click+=newSystem.EventHandler(this.button4_Click);

//

//button5

//

this.button5.Anchor=((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom|System.Windows.Forms.AnchorStyles.Left)));

this.button5.Location=newSystem.Drawing.Point(272,488);

this.button5.Name="button5";

this.button5.TabIndex=5;

this.button5.Text="Example4";

this.button5.Click+=newSystem.EventHandler(this.button5_Click);

//

//button6

//

this.button6.Anchor=((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom|System.Windows.Forms.AnchorStyles.Left)));

this.button6.Location=newSystem.Drawing.Point(360,488);

this.button6.Name="button6";

this.button6.TabIndex=6;

this.button6.Text="Example5";

this.button6.Click+=newSystem.EventHandler(this.button6_Click);

//

//button7

//

this.button7.Anchor=((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom|System.Windows.Forms.AnchorStyles.Left)));

this.button7.Location=newSystem.Drawing.Point(448,488);

this.button7.Name="button7";

this.button7.TabIndex=7;

this.button7.Text="Example6";

this.button7.Click+=newSystem.EventHandler(this.button7_Click);

//

//Form1

//

this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);

this.ClientSize=newSystem.Drawing.Size(728,517);

this.Controls.Add(this.button7);

this.Controls.Add(this.button6);

this.Controls.Add(this.button5);

this.Controls.Add(this.button4);

this.Controls.Add(this.button3);

this.Controls.Add(this.button2);

this.Controls.Add(this.button1);

this.Controls.Add(this.listBox1);

this.Name="Form1";

this.Text="XMLReader";

this.ResumeLayout(false);

//

//xmlPath

//

this.xmlPath="sample.xml";

}

#endregion

///

///应用程序的主入口点。

///

[STAThread]

staticvoidMain()

{

Application.Run(newForm1());

}

privatevoidbutton1_Click(objectsender,System.EventArgse)

{

xRead=newXmlReader(this.xmlPath,this.listBox1);

try

{

xRead.EachXml();

}

catch(XmlExceptionxmlExp)

{

MessageBox.Show(xmlExp.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);

}

catch(Exceptionexp)

{

MessageBox.Show(exp.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);

}

finally

{

xRead.Dispose();

}

}

privatevoidbutton2_Click(objectsender,System.EventArgse)

{

xRead=newXmlReader(this.xmlPath,this.listBox1);

try

{

xRead.ReadXmlByNodeType();

}

catch(XmlExceptionxmlExp)

{

MessageBox.Show(xmlExp.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);

}

catch(Exceptionexp)

{

MessageBox.Show(exp.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);

}

finally

{

xRead.Dispose();

}

}

privatevoidbutton3_Click(objectsender,System.EventArgse)

{

XmlNodeType[]xmlNType={XmlNodeType.Element,XmlNodeType.EndElement,XmlNodeType.XmlDeclaration};

xRead=newXmlReader(this.xmlPath,this.listBox1);

try

{

xRead.FilterByNodeType(xmlNType);

}

catch(XmlExceptionxmlExp)

{

MessageBox.Show(xmlExp.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);

}

catch(Exceptionexp)

{

MessageBox.Show(exp.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);

}

finally

{

xRead.Dispose();

}

}

privatevoidbutton4_Click(objectsender,System.EventArgse)

{

xRead=newXmlReader(this.xmlPath,this.listBox1);

try

{

xRead.ReadXmlTextvalue();

}

catch(XmlExceptionxmlExp)

{

MessageBox.Show(xmlExp.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);

}

catch(Exceptionexp)

{

MessageBox.Show(exp.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);

}

finally

{

xRead.Dispose();

}

}

privatevoidbutton5_Click(objectsender,System.EventArgse)

{

xRead=newXmlReader(this.xmlPath,this.listBox1);

try

{

xRead.ReadXmlAttributes();

}

catch(XmlExceptionxmlExp)

{

MessageBox.Show(xmlExp.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);

}

catch(Exceptionexp)

{

MessageBox.Show(exp.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);

}

finally

{

xRead.Dispose();

}

}

privatevoidbutton6_Click(objectsender,System.EventArgse)

{

xRead=newXmlReader(this.xmlPath,this.listBox1);

try

{

xRead.ReadXmlNamespace();

}

catch(XmlExceptionxmlExp)

{

MessageBox.Show(xmlExp.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);

}

catch(Exceptionexp)

{

MessageBox.Show(exp.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);

}

finally

{

xRead.Dispose();

}

}

privatevoidbutton7_Click(objectsender,System.EventArgse)

{

xRead=newXmlReader(this.xmlPath,this.listBox1);

try

{

xRead.ReadXml();

}

catch(XmlExceptionxmlExp)

{

MessageBox.Show(xmlExp.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);

}

catch(Exceptionexp)

{

MessageBox.Show(exp.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);

}

finally

{

xRead.Dispose();

}

}

}

}

以下是用于测试的XML文件:

在项目中新建一个XML文件取名为sample.xml,建好后把该文件拷到项目的bin目录下的Debug目录下

<?xmlversion="1.0"encoding="utf-8"?>

<?XML:NAMESPACE PREFIX = CAT />WineDivisionSouth

BeerDivisionNorth

<?XML:NAMESPACE PREFIX = ITEMSCAT />

RancaguaWhile

RancaguaRed

ChillanRed

RancaguaWhile

SantiagoRed

RancaguaWhile

RancaguaRed

RancegaoRed

SutothadBlack

BlackNmeBlue

BooklistRed

RancegaoWhite

="2">

时间: 2024-08-21 08:52:24

C#中使用XML——读取XML的相关文章

Linq to XML 读取XML 备忘笔记

本文转载:http://www.cnblogs.com/infozero/archive/2010/07/13/1776383.html Linq to XML 读取XML 备忘笔记 最近一个项目中有要用到 xml 读取,我首先想到的是使用 Linq 读取(XML 解析感觉有点麻烦),项目完成,现抽取其内容,作如下笔记备忘. 1. demo.xml demo.xml <?xml version="1.0" encoding="utf-8" ?><n

PHP中使用xmlreader读取xml数据示例_php技巧

有一个XML文件,内容如下: 复制代码 代码如下: <?xml version="1.0"?>  <shows>      <show>          <name>Simpsons</name>          <channel>FOX</channel>          <start>8:00 PM</start>          <duration>30

php中对xml读取的相关函数的介绍一_php技巧

对象 XML解析函数 描述  元素 xml_set_element_handler() 元素的开始和结束  字符数据 xml_set_character_data_handler() 字符数据的开始  外部实体 xml_set_external_entity_ref_handler() 外部实体出现  未解析外部实体 xml_set_unparsed_entity_decl_handler() 未解析的外部实体出现  处理指令 xml_set_processing_instruction_han

使用XmlDocument读取XML节点所有数据

网上有好多ASP.NET读取XML的例子,比如使用Dataset来读取,但本文教程却是使用XmlDocument来读取XML节点下所有数据,我们先来看下这个XML格式:SysRightsDb.xml  XML Code <?xml version="1.0" encoding="utf-8" ?> <root> <rights name="SYS"> <xml name="股票行情"

PHP读取xml方法介绍

一,什么是xml,xml有什么用途 XML(Extensible Markup Language)即可扩展标记语言,它与HTML一样,都是SGML(Standard Generalized Markup Language,标准通用标记语言).Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具.扩展标记语言XML是一种简单的数据存储语言,使用一系列简单的标记描述数据,而这些标记可以用方便的方式建立,虽然XML占用的空间比二进制数据要占用更多的空间,但XML

C#中使用XML——编写XML

xml 在我的上一篇文章<C#中使用XML--读取XML>中和大家讨论了如何使用.NET Framework中提供的类在C#中读取XML以及读取的一些相关概念,那么今天就说一说如何在C#中编写XML文档,起初我觉得用编程的方式去编写XML简直就是自讨苦吃,后来想想还是觉得挺有用的,我想Microsoft那班家伙能编出这些类来应该不是仅仅为了向比尔i盖茨交差吧!至于它的用处嘛--比如说做安装程序啊!我们可以根据在安装过程中用户所选的选项以及一些设置来生成相应的XML文档再根据XML文档来初始化我

使用LINQ to XML来读取XML文档

用于XML的语言级集成查询(LINQ to XML)允许使用标准查询操作符就像树形操作符一样来查询XML数据,它能够提供类似XPath的导航在后代.祖先和兄弟的XML元素中导航.它简化了对XML数据的使用,不必使用额外的语言语法像XPath或XQuery.你可以使用LINQ to XML对你从文件系统.从一个远程web service或从一个内存中的XML内容中获得的XML执行LINQ查询.这篇文章将只关注于使用LINQ从一个XML文件--Customers.xml文件查询XML. 创建一个简单

php xmlreader simplexml DOMDocument等读取xml的例子

要处理 XML 文件,有两种传统的处理思路: SAX 和 DOM . SAX 基于事件触发机制,对 XML 文件进行一次扫描,完成要进行的处理: DOM 则将整个 XML 文件构造为一棵 DOM树,通过对 DOM 树的遍历完成处理.这两种方法各有优缺点, SAX 的处理思路相对抽象,DOM 的处理过程相对烦琐,都不很适合新手的入门.PHP5 推出了一套新的 XML 处理函数,即 SimpleXML .名如其实, SimpleXML 本身小巧精干,只提供了少量的几个方法函数,但用它处理起 XML

PHP读取xml方法介绍_php技巧

一,什么是xml,xml有什么用途 XML(Extensible Markup Language)即可扩展标记语言,它与HTML一样,都是SGML(Standard Generalized Markup Language,标准通用标记语言).Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具.扩展标记语言XML是一种简单的数据存储语言,使用一系列简单的标记描述数据,而这些标记可以用方便的方式建立,虽然XML占用的空间比二进制数据要占用更多的空间,但XML