惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

AI
AI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Google DeepMind News
Google DeepMind News
T
Tenable Blog
博客园_首页
S
Securelist
Spread Privacy
Spread Privacy
Google Online Security Blog
Google Online Security Blog
Forbes - Security
Forbes - Security
Engineering at Meta
Engineering at Meta
U
Unit 42
L
LINUX DO - 热门话题
量子位
T
Threat Research - Cisco Blogs
博客园 - 【当耐特】
C
Cyber Attacks, Cyber Crime and Cyber Security
K
Kaspersky official blog
MyScale Blog
MyScale Blog
P
Proofpoint News Feed
The Last Watchdog
The Last Watchdog
Google DeepMind News
Google DeepMind News
GbyAI
GbyAI
Martin Fowler
Martin Fowler
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Security Latest
Security Latest
Scott Helme
Scott Helme
V
Vulnerabilities – Threatpost
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
InfoQ
Know Your Adversary
Know Your Adversary
Cisco Talos Blog
Cisco Talos Blog
The Register - Security
The Register - Security
T
The Blog of Author Tim Ferriss
aimingoo的专栏
aimingoo的专栏
V2EX - 技术
V2EX - 技术
T
Tailwind CSS Blog
月光博客
月光博客
Recent Announcements
Recent Announcements
G
Google Developers Blog
F
Full Disclosure
W
WeLiveSecurity
宝玉的分享
宝玉的分享
腾讯CDC
G
GRAHAM CLULEY
Vercel News
Vercel News
Simon Willison's Weblog
Simon Willison's Weblog
美团技术团队
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security

博客园 - iCeSnaker

[转载]两个未公开的ACCESS方法的使用技巧 Microsoft SQL Reporting Services – Running a Report from the Command Line Microsoft AntiSpyware Beta1 发布了,贴几张图片上来 大型组图:微软总部印象 两个桌面主题 [讨论] 制作博客园年刊 用C#实现生成PDF文档 C# Delegate 简介 单元测试的基本方法 小软件项目开发的管理 C#中为DataGrid添加下拉列表框 什么是需求? 呐喊 -- 希望博客圆一直是世外桃源 将sql server中的数据倒入Excel(c#) 在.net中轻松掌握Windows窗体间的数据交互(三) 在.net中轻松掌握Windows窗体间的数据交互(二) 在.net中轻松掌握Windows窗体间的数据交互(一) C# 编码规范和编程好习惯 C#实现的基本算法
用C#读取XML文档
iCeSnaker · 2004-09-12 · via 博客园 - iCeSnaker

作者: 深蓝色系统
发布日期:2002-07-30 | 更新日期:2004-07-02
本文将以一个非常简单的例子来说明如何使用C#访问一个XML文件并且读取其中的信息。例子本身并无任何实际意义,它只是简单的介绍了如何调用微软的XML标准以及如何运用到实际当中去。希望能够对初次接触C#或者未尝试过通过C#读取XML文件的读者有所启发。本文旨在抛砖引玉,希望能与更多的朋友交流和分享经验。

    制作过程

1.  运行Visual Studio.NET,新建Visual C#.NET工程,这里取名为ReadXML。
2.  在解决方案资源管理器中,将Form1.cs改名为frmAuthor.cs(此项为可选)。
3.  将Form1的Name属性改为frmAuthor,Text属性改为Read XML Document,Font改为Verdana,9pt(此项为可选)。
4.  在窗体上添加四个控件:Lable控件(Name: lbl、Text: Author Name:)、ComboBox控件(Name: cboAuthor、DropDownStyle: DropDownList)、RichTextBox控件(Name: richtxt、Text:空)、Button控件(Name: btnShow、Text: Show Author’s Info)。调整各控件的位置,此时用户界面类似于下图:

5.  双击窗体,则IDE自动切换到代码编辑窗口,并自动添加了窗体载入方法frmAuthor_Load。在该方法内加入如下代码:
this.cboAuthor.Items.Add("张爱铃");
this.cboAuthor.Items.Add("福楼拜");
this.cboAuthor.Items.Add("马克·吐温"); 
 以上代码添加几条作者名字到ComboBox中去,这些作者的信息在XML文档中有对应项。

    OK,到这里为止,我们所要做的一些程序初始化工作就已经完毕了。下面让我们来看看本例程所要展示的主要知识点。
    在编写访问代码之前,让我们来看看XML文档的内容,如下:

<?xml version="1.0" encoding="gb2312" ?> 
    <Author>
        <Zhang>
            <Intro>省略</Intro> 
        </Zhang>
        <Fu>
            <Intro>省略</Intro> 
        </Fu>
        <Mark>
            <Intro>省略</Intro> 
        </Mark>
    </Author> 

    这是一个简单的XML文档,每个作者对应一条信息(Intro),共三条信息。我们要做的事情就是通过程序、按照XML文档的结构读取作者的简介,也就是Intro部分的内容。OK,下面就让我们来实现具体的代码。

1.  将以上XML文档(取名为Author.xml)放置在工程的根目录下(事实上放在任何地方均可以,但为了便于管理,这里就直接放在工程根目录下)。然后回到VS.NET,在frmAuthor.cs代码中头部添加以下代码:
using System.Xml;
    用于引用微软System.Xml命名空间。
2.  回到frmAuthor.cs[设计]页,双击Show Author’s Info按钮,则IDE自动添加btnShow_Click事件在frmAuthor.cs里。在btnShow_Click事件中,填写以下代码:
try
{
//Declaration
string strAuthor = this.cboAuthor.Text.Trim();
string strXMLAuthor = "";
string strInfo;
XmlDocument doc = new XmlDocument(); 
System.Xml.XPath.XPathNavigator nav;
// = new System.Xml.XPath.XPathNavigator();
System.Xml.XPath.XPathNodeIterator iterator;
// = new System.Xml.XPath.XPathNodeIterator();

//Validation
if( strAuthor == "")
{
throw new ArgumentException("Author","You must select a author name!");
}

//Load XML document
doc.Load("..\\..\\Author.xml");

//Set nav object
nav = ((System.Xml.XPath.IXPathNavigable)(doc)).CreateNavigator();

//Justification
if( strAuthor == "张爱铃" ) strXMLAuthor = "Zhang";
else if( strAuthor == "福楼拜" ) strXMLAuthor = "Fu";
else if( strAuthor == "马克·吐温" ) strXMLAuthor = "Mark";

//Set node iterator
iterator = nav.Select("Author/" + strXMLAuthor);

//Move to the desired node
iterator.MoveNext();

//Get the value of current node
strInfo = iterator.Current.Value;

//Display author's information
this.richtxt.Text = strInfo;
}
catch(System.Exception err)
{
//Display Error
MessageBox.Show(err.Message, "ERROR!");
} 
3.  主要代码我们已经填写完毕。下面使用菜单“生成”>“生成解决方案”命令后,按F5即可运行测试程序是否正确了。

解释部分

    关于System.Xml命名空间及其子命名空间,可以查阅2002年后的MSDN Library,其中的“命名空间层次结构”一节中,我们可以清楚地看到各System.Xml命名空间的层次结构。

    以上通过一个非常简单的例子来向大家展示如何利用C#及System.Xml命名空间直接访问一个XML文档。当然,本例程中的代码只是一种实现方法,代码也未经过整理和优化,这里只是希望能够通过这个例程给大家展示Visual C#的一个知识点而已,希望能有更多的朋友提供更好的意见和建议。联系方式:

epubcn@msn.com,个人网站:http://www.epubcn.com