[英]how to insert and remove Xml document fragment between nodes?
如何在節點之間插入和刪除Xml文檔片段。我想在特定標記內添加和刪除xml片段。
XML:
<project>
<ItemGroup>
<Content Include="App_LocalResources" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="lice.pccx" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0" />
</project>
我想在<ItemGroup>
插入<EmbeddedResource Include=licenses.licx />
片段
之后我想刪除<EmbeddedResource Include="lice.pccx" />
。
var xDoc = XElement.Load("FilePath");
if (xDoc == null)
return;
var myNewElement = new XElement("ElementName"
new XAttribute("AttributeName", value1),
new XAttribute("AttributeName", value2)
//And so on ...
);
xDoc.Add(myNewElement);
xDoc.Save("FilePath");
//
doc.Root.Descendants(actualNode.Parent.Name)
.Elements(actualNode.Name)
.Remove();
using System.Xml.XPath;
var xml = XElement.Load(xmlFile);
//var xml = XElement.Parse(xmlString); //in case of loading from string
var liceElement = xml.XPathSelectElement("//EmbeddedResource[@Include='lice.pccx']");
liceElement.Remove();
//liceElement.Parent.Remove(); //if you would like to remove the whole 'ItemGroup'
xml.Add(new XElement("ItemGroup",
new XElement("EmbeddedResource",
new XAttribute("Include", "licenses.licx"))));
xml.ToString();
在這里,您可以找到一些有用的xpath示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.