积极答复者
linq to xml的删除问题

问题
-
请教高手,我想在xml数据中找到特点某个节点,将其删除,比如
<ArrayOfPlantSpecies xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<PlantSpecies>
<ExtensionData />
<PlantID>1</PlantID>
<PlantSpeciesID>3</PlantSpeciesID>
<PlantSpeciesInfo>增加后的抗旱力强</PlantSpeciesInfo>
<PlantSpeciesName>优航三号!</PlantSpeciesName>
</PlantSpecies>
<PlantSpecies>
<ExtensionData />
<PlantID>2</PlantID>
<PlantSpeciesID>1</PlantSpeciesID>
<PlantSpeciesInfo>修改以后的耐寒力强</PlantSpeciesInfo>
<PlantSpeciesName>修改以后的优航2号</PlantSpeciesName>
</PlantSpecies>
<PlantSpecies>
<ExtensionData />
<PlantID>2</PlantID>
<PlantSpeciesID>4</PlantSpeciesID>
<PlantSpeciesInfo>瓜大味甜</PlantSpeciesInfo>
<PlantSpeciesName>西瓜1号</PlantSpeciesName>
</PlantSpecies>
</ArrayOfPlantSpecies>
在这个xml数据集中 我想删除plantID为2的所有plantSpecies节点,我该怎么做,用linq to xml语法,
答案
-
你好!
欢迎来到MSDN中文论坛。
这儿有个例子,是关于删除XML的一个特定值得节点的:
IEnumerable<XElement> result = from s in x.Element("channel").Elements("item)
where s.Element("plantID").Value == 2
select s;
result.Remove();x.Element("channel").Elements("item) 就是刪除item以下的所有子节点。
另外,下面这个链接是LINQ to XML讲删除的一个例子,可供你参考!
http://dev.firnow.com/course/4_webprogram/asp.net/asp_netxl/2008430/112121.html
希望能对你有帮助!
Jackie Sun [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 wizardhy 2011年3月1日 6:15
全部回复
-
你好!
欢迎来到MSDN中文论坛。
这儿有个例子,是关于删除XML的一个特定值得节点的:
IEnumerable<XElement> result = from s in x.Element("channel").Elements("item)
where s.Element("plantID").Value == 2
select s;
result.Remove();x.Element("channel").Elements("item) 就是刪除item以下的所有子节点。
另外,下面这个链接是LINQ to XML讲删除的一个例子,可供你参考!
http://dev.firnow.com/course/4_webprogram/asp.net/asp_netxl/2008430/112121.html
希望能对你有帮助!
Jackie Sun [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 wizardhy 2011年3月1日 6:15