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

推荐订阅源

Martin Fowler
Martin Fowler
Blog — PlanetScale
Blog — PlanetScale
Vercel News
Vercel News
L
LangChain Blog
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
D
DataBreaches.Net
云风的 BLOG
云风的 BLOG
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
About on SuperTechFans
博客园_首页
N
Netflix TechBlog - Medium
Y
Y Combinator Blog
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
B
Blog
Apple Machine Learning Research
Apple Machine Learning Research
罗磊的独立博客
美团技术团队
V
V2EX

博客园 - xumingming

C# 长浏览 SQL Server 的位运算 sql 的随机函数newID()和RAND() sp_executesql的用法 Sql Server 存储过程分页 linq 存储过程返回多个结果集 Sql语句访问WEB 一些常用的类 MVC Controller与ActionResult的返回值 - xumingming - 博客园 IEnumerable_T_、IEnumerable、ICollection_T_、IList_T_、ObservableCollectin_T_和Collection_T_得关系 routes.MapRoute - xumingming - 博客园 JQuery与Ajax常用代码 MVC 小笔记 - xumingming - 博客园 GridView 的临时增删改查示例 GridView 72般绝技 C#代码中的事务 - xumingming - 博客园 VS2005中将GridView 中的数据导出至excel - xumingming - 博客园 触发器 删除数据库表中的信息(适用于被注入)
Linq To Xml (增,删,改,查) - xumingming
xumingming · 2010-05-17 · via 博客园 - xumingming

XML数据如下:

<?xml version="1.0" encoding="utf-8"?>   
<DataSet>   
   <dbName ID="b785723f-e7ef-448a-81b2-527de05f422a" Name="aaaaaa" Remarks="r-aa" />   
   <dbName ID="53232faa-a069-46b1-8ff4-f5bf47db7a68" Name="bbbbb" Remarks="r-bbbb" />   
</DataSet>

一、查询
程序代码:

XElement root = XElement.Load("Test.xml");                //加载需要操作的XML文件
IEnumerable<XElement> tests =
     from el in root.Elements("dbName")                //查询dbName节点
     where (string)el.Attribute("Remarks") == "r-aa"    //条件:节点的Remarks属性的值为 r-aa
     select el;
foreach (XElement el in tests)                            //遍历查询结果
{
     Response.Write((string)el.Attribute("Name") + "<br>");   //输出查询节点的Name属性的值
}

输出结果:
aaaaaa

二、修改

1、添加节点:
程序代码:

XElement mXml = XElement.Load("Test.xml");                //加载需要操作的XML文件
xmlTree.Add(new XElement("newNode", "newNodes"));          //添加一个名为newNode,值为newNodes的节点

输出结果:
<?xml version="1.0" encoding="utf-8"?>   
<DataSet>   
   <dbName ID="b785723f-e7ef-448a-81b2-527de05f422a" Name="aaaaaa" Remarks="r-aa" />   
   <dbName ID="53232faa-a069-46b1-8ff4-f5bf47db7a68" Name="bbbbb" Remarks="r-bbbb" />  
   <newNode>newNodes</newNode>  
</DataSet>

2、删除节点
程序代码:

XElement root = XElement.Load("Test.xml");                //加载需要操作的XML文件
IEnumerable<XElement> tests =
     from el in root.Elements("dbName")                //查询dbName节点
     where (string)el.Attribute("Remarks") == "r-aa"    //条件:节点的Remarks属性的值为 r-aa
     select el;
foreach (XElement el in tests)                            //遍历查询结果
{
     el.RemoveNodes();   //删除查询节点的Name属性的值
}

输出结果:
<?xml version="1.0" encoding="utf-8"?>   
<DataSet>      
   <dbName ID="53232faa-a069-46b1-8ff4-f5bf47db7a68" Name="bbbbb" Remarks="r-bbbb" />  
</DataSet>

3、修改节点
程序代码:

XElement root = XElement.Load("Test.xml");                //加载需要操作的XML文件
IEnumerable<XElement> tests =
     from el in root.Elements("dbName")                //查询dbName节点
     where (string)el.Attribute("Remarks") == "r-aa"    //条件:节点的Remarks属性的值为 r-aa
     select el;
foreach (XElement el in tests)                            //遍历查询结果
{
     el.SetValue("cccc");   //删除查询节点的Name属性的值
}

输出结果:
<?xml version="1.0" encoding="utf-8"?>   
<DataSet>      
   <dbName ID="b785723f-e7ef-448a-81b2-527de05f422a" Name="cccc" Remarks="r-aa" />
   <dbName ID="53232faa-a069-46b1-8ff4-f5bf47db7a68" Name="bbbbb" Remarks="r-bbbb" />  
</DataSet>

XElement 关于修改和删除的方法还有很多,我这里就不一一列举了。