本文实例总结了C#读取xml节点数据的方法。分享给大家供大家参考。具体如下:

第一种:
使用XPath
XML的路径我配置在web.config 的appSettings节点下

<appSettings>
<add key="orgCodePath" value="../../template/home/orgCode.xml"/>
</appSettings>

XML结构如下:

<?xml version="1.0" encoding="utf-8" ?>
<Organizations>
<Organization>
<ID>1111</ID>
<DomainName>aa</DomainName>
</Organization>
<Organization>
<ID>2222</ID>
<DomainName>bb</DomainName>
</Organization>
</Organizations>

在C#中,我使用HashTable来进行存储:

Hashtable ht = new Hashtable();
string orgCodePath = Server.MapPath(ConfigurationSettings.AppSettings["orgCodePath"]);
//string orgCodePath = Server.MapPath("../../template/home/orgCode.xml");
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(orgCodePath);
//获取节点列表
XmlNodeList topM = xmldoc.SelectNodes("//Organization");
foreach (XmlElement element in topM)
{
string id = element.GetElementsByTagName("ID")[].InnerText;
string domainName = element.GetElementsByTagName("DomainName")[].InnerText;
ht.Add(id, domainName);
}

第二种:

遍历式读取XML

//打开某文件(假设web.config在根目录中)
string filename=Server.MapPath("/") + @"WebApplication1/web.config";
XmlDocument xmldoc= new XmlDocument();
xmldoc.Load(filename);
//得到顶层节点列表
XmlNodeList topM=xmldoc.DocumentElement.ChildNodes;
foreach(XmlElement element in topM)
{
if(element.Name.ToLower()=="appsettings")
{
//得到该节点的子节点
XmlNodeList nodelist=element.ChildNodes;
if ( nodelist.Count > )
{
//DropDownList1.Items.Clear();
foreach(XmlElement el in nodelist)//读元素值
{
//DropDownList1.Items.Add(el.Attributes["key"].InnerXml);
//this.TextBox2.Text=el.Attributes["key"].InnerText;
this.TextBox2.Text=el.Attributes["key"].Value;
this.Label1.Text=el.Attributes["value"].Value;
//同样在这里可以修改元素值,在后面save。
// el.Attributes["value"].Value=this.TextBox2.Text;
}
}
}
}
xmldoc.Save(filename);

在某节点下增加一个元素,并设置值:

if(element.Name.ToLower()=="appsettings")
{
XmlElement elem =xmldoc.CreateElement("add");
element.AppendChild(elem);
elem.InnerText="ltp";
xmldoc.Save(filename);
}

效果:

<appSettings>
<add key="密码" value="admin" />
<add>ltp</add>
</appSettings>

在某节点下增加一个元素,并增加两个属性:

if(element.Name.ToLower()=="appsettings")
{
XmlElement elem =xmldoc.CreateElement("add");
element.AppendChild(elem);
XmlAttribute xa=xmldoc.CreateAttribute("key");
xa.Value="ltp";
XmlAttribute xa2=xmldoc.CreateAttribute("value");
xa2.Value="first";
elem.SetAttributeNode(xa);
elem.SetAttributeNode(xa2);
xmldoc.Save(filename);
}

效果:

<appSettings>
<add key="密码" value="admin" />
<add key="ltp" value="first" />
</appSettings>

添加空元素:

XmlNode node=doc.CreateElement(groupname);
node.InnerText="";
doc.LastChild.AppendChild(node);
doc.Save(xmlfile);

删除一个节点元素:

string itemname=this.listBox1.SelectedItem.ToString();
this.listBox1.Items.Remove(this.listBox1.SelectedItem);
//begin del xmlfile
XmlDocument doc=new XmlDocument();
doc.Load(xmlfile);
XmlNodeList topM=doc.DocumentElement.ChildNodes;
foreach(XmlElement element in topM)
{
if(element.Name==this.comboBox1.Text)
{
//得到该节点的子节点
XmlNodeList nodelist=element.ChildNodes;
foreach(XmlElement el in nodelist)//读元素值
{
if(el.Attributes["key"].Value==itemname)
{
element.RemoveChild(el);
}
}//循环元素
}//得到组
}//循环组
doc.Save(xmlfile); //一定要保存一下,否则不起作用
//筛选数据
private void Reader_Xml(string pathFlie)
{
XmlDocument Xmldoc=new XmlDocument();
Xmldoc.Load(pathFlie);
XmlNodeList Record1=Xmldoc.DocumentElement.SelectNodes(Code[@id='']);
int f=;
foreach(XmlNode xnode in Record1)
{
}
}

读取xml数据,  两种xml方式

<aaa>
<bb>something</bb>
<cc>something</cc>
</aaa>
<aaa>
<add key="123" value="321"/>
</aaa>

第一种方法:

DS.ReadXml("your xmlfile name");
Container.DataItem("bb");
Container.DataItem("cc");
DS.ReadXmlSchema("your xmlfile name");

第二种方法:

<aaa>
<add key="123" value="321"/>
</aaa>

如果我要找到123然后取到321应该怎么写呢?

using System.XML;
XmlDataDocument xmlDoc = new System.Xml.XmlDataDocument();
xmlDoc.Load(@"c:/Config.xml");
XmlElement elem = xmlDoc.GetElementById("add");
string str = elem.Attributes["value"].Value

第三种方法:  SelectSingleNode  读取两种格式的xml :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<ConnectionString>Data Source=yf; user id=ctm_dbo;password=123</ConnectionString>
</appSettings>
</configuration>
XmlDocument doc = new XmlDocument();
doc.Load(strXmlName);
XmlNode node=doc.SelectSingleNode("/configuration/appSettings/ConnectionString");
if(node!=null)
{
string k1=node.Value; //null
string k2=node.InnerText;//Data Source=yf; user id=ctm_dbo;password=123
string k3=node.InnerXml;//Data Source=yf; user id=ctm_dbo;password=123
node=null;
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value="Data Source=yf; user id=ctm_dbo;password=123" />
</appSettings>
</configuration>
XmlNode node=doc.SelectSingleNode("/configuration/appSettings/add");
if(node!=null)
{
string k=node.Attributes["key"].Value;
string v=node.Attributes["value"].Value;
node=null;
}
XmlNode node=doc.SelectSingleNode("/configuration/appSettings/add");
if(node!=null)
{
XmlNodeReader nr=new XmlNodeReader(node);
nr.MoveToContent();
//检查当前节点是否是内容节点。如果此节点不是内容节点,则读取器向前跳至下一个内容节点或文件结尾。
nr.MoveToAttribute("value");
string s=nr.Value;
node=null;
}

C#读取xml节点数据方法小结的更多相关文章

  1. C#中经常使用的几种读取XML文件的方法

    XML文件是一种经常使用的文件格式,比如WinForm里面的app.config以及Web程序中的web.config文件,还有很多重要的场所都有它的身影.Xml是Internet环境中跨平台的,依赖 ...

  2. C#三种常用的读取XML文件的方法

    下面我将介绍三种常用的读取XML文件的方法.分别是 1: 使用 XmlDocument 2: 使用 XmlTextReader 3: 使用 Linq to Xml 这里我先创建一个XML文件,名为Bo ...

  3. 用LINQ获取XML节点数据

    Insus.NET想对<从字符串中获取XML节点数据> http://www.cnblogs.com/insus/p/3299052.html 这篇改写为使用LINQ的方法实现.LINQ中 ...

  4. 从字符串中获取XML节点数据

    从字符串中获取XML节点数据,前一篇<字符串创建XML文档> http://www.cnblogs.com/insus/p/3298579.html 是储存为一个XML文档.现在,Insu ...

  5. 在C#程序中,创建、写入、读取XML文件的方法

    一.在C#程序中,创建.写入.读取XML文件的方法 1.创建和读取XML文件的方法,Values为需要写入的值 private void WriteXML(string Values) { //保存的 ...

  6. C#中常用的几种读取XML文件的方法

    1.C#中常用的几种读取XML文件的方法:http://blog.csdn.net/tiemufeng1122/article/details/6723764/

  7. C#读取XML文件的方法

    先写一个xml文件: <?xml version="1.0" encoding="utf-8" ?> <bookste> <!-- ...

  8. flash读取XML节点内容以及节点属性

    原文地址:http://hi.baidu.com/yqzdm/item/f95fd9d24679d916d90e44c9 一.xml的写法: 这里的xml只是在有限范围内的了解,限于写一些简单的用于f ...

  9. C# 递归读取XML菜单数据

    在博客园注册了有4年了,很遗憾至今仍未发表过博客,趁周末有空发表第一篇博客.小生不才,在此献丑了! 最近在研究一些关于C#的一些技术,纵观之前的开发项目的经验,做系统时显示系统菜单的功能总是喜欢把数据 ...

随机推荐

  1. c++ primer 5th(中文版)勘误

    \(P_{158}\) "末位大于 3" 改为 "末位大于等于 3" \(P_{302}\) \(P_{319}\) // 添加元素用光多余容量 while ( ...

  2. 软件安装:树上分组DP/tarjan缩点/(也许基环树?)

    提炼:tarjan环缩成点,建0虚根,跑树形DP,最难的是看出可能有n个点n条边然后缩点,n个点n条边可能不只有一个环 n个点n条边->基环树: 基环树,也是环套树,简单地讲就是树上在加一条边. ...

  3. 虚拟机安装 Output error file to the following location

    有的用户会对你中安装虚拟机系统,但偶尔会在安装过程中遇到一些问题.比如在电脑安装虚拟机系统时出现提示“Output error file to the following location”,这一般是 ...

  4. 在 Cloudera Data Flow 上运行你的第一个 Flink 例子

    文档编写目的 Cloudera Data Flow(CDF) 作为 Cloudera 一个独立的产品单元,围绕着实时数据采集,实时数据处理和实时数据分析有多个不同的功能模块,如下图所示: 图中 4 个 ...

  5. 12.python csv文件写入和读出

    import csv headers = ["class", "name", "sex", "height", &quo ...

  6. 使用 XSLT 显示 XML

    通过使用 XSLT,您可以向 XML 文档添加显示信息. 使用 XSLT 显示 XML XSLT 是首选的 XML 样式表语言. XSLT (eXtensible Stylesheet Languag ...

  7. 将Emacs Org mode用于GTD任务管理

    在上一篇日志中,我简要介绍了如何围绕Emacs Org mode构建个人任务管理系统的基本思路与方法.因为Org mode体系庞大.功能繁杂,本文仅以提纲契领的方式介绍不同环节在Org mode中的操 ...

  8. sublime安装完插件后出现的一些问题

    1.安装anaconda后代码前面出现小方框 解决办法:这是由于不符合PEP8代码规范,在空白地方右击,选择anaconda --> autoformat PEP8 Errors ,同时保证导入 ...

  9. 浅谈 Catalan number——卡特兰数

    一.定义: 卡特兰数是一组满足下面递推关系的数列: 二.变形: 首先,设h(n)为Catalan数的第n+1项,令h(0)=1,h(1)=1,Catalan数满足递推式: h(n)= h(0)*h(n ...

  10. 织梦DedeCms技术资料

    Dedecms调用文章发布时间的方法 11-20 样式 ([field:pubdate function='strftime("%m-%d",@me)'/]) May 15, 20 ...