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

推荐订阅源

S
Secure Thoughts
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tenable Blog
Project Zero
Project Zero
T
The Exploit Database - CXSecurity.com
T
Threat Research - Cisco Blogs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyberwarzone
Cyberwarzone
PCI Perspectives
PCI Perspectives
G
GRAHAM CLULEY
H
Hacker News: Front Page
Cloudbric
Cloudbric
Latest news
Latest news
N
News and Events Feed by Topic
C
CERT Recently Published Vulnerability Notes
Attack and Defense Labs
Attack and Defense Labs
SecWiki News
SecWiki News
Security Latest
Security Latest
MyScale Blog
MyScale Blog
阮一峰的网络日志
阮一峰的网络日志
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Security Archives - TechRepublic
Security Archives - TechRepublic
V2EX - 技术
V2EX - 技术
B
Blog RSS Feed
L
LINUX DO - 最新话题
人人都是产品经理
人人都是产品经理
Last Week in AI
Last Week in AI
IT之家
IT之家
Jina AI
Jina AI
Y
Y Combinator Blog
博客园 - 聂微东
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
Visual Studio Blog
P
Privacy International News Feed
B
Blog
S
Schneier on Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
Help Net Security
Help Net Security
D
DataBreaches.Net
博客园_首页
G
Google Developers Blog
I
InfoQ
量子位
大猫的无限游戏
大猫的无限游戏
S
Security @ Cisco Blogs

博客园 - 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