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

推荐订阅源

S
SegmentFault 最新的问题
Google DeepMind News
Google DeepMind News
G
Google Developers Blog
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
月光博客
月光博客
Jina AI
Jina AI
宝玉的分享
宝玉的分享
人人都是产品经理
人人都是产品经理
D
DataBreaches.Net
V
V2EX
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
Last Week in AI
Last Week in AI
B
Blog
博客园 - 叶小钗
小众软件
小众软件
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
A
About on SuperTechFans
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog

博客园 - 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 关于修改和删除的方法还有很多,我这里就不一一列举了。