如何使用 C# 从 XML 文件中删除节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/919645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete node from XML file using C#
提问by SyncMaster
Possible Duplicate:
How to remove an XmlNode from XmlNodeList
Hi, How can i delete a set of nodes from an XML file.? Here is a code snippet.
嗨,我如何从 XML 文件中删除一组节点。?这是一个代码片段。
string path = @"C:\Documents and Settings\e454935\Desktop\NUnitSettings.xml";
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
xmldoc.Load(fs);
fs.Close();
xmldoc.DocumentElement.RemoveChild(xmldoc.DocumentElement.ChildNodes[1]);
FileStream WRITER = new FileStream(path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
xmldoc.Save(WRITER);
WRITER.Close();
I tried the following code simply to delete a node and got "Object reference not set to an instance of an object." at
我尝试了以下代码只是为了删除一个节点并得到“未将对象引用设置为对象的实例”。在
xmldoc.DocumentElement.RemoveChild(xmldoc.DocumentElement.ChildNodes[1]);
Here is a sample XML file,
这是一个示例 XML 文件,
<?xml version="1.0"?>
<Xml1>
<Settings>
<Setting name="DisplayFormat" value="Full" />
<Setting name="File1" value="a" />
<Setting name="File1" value="b" />
<Setting name="File1" value="c" />
<Setting name="File1" value="d" />
</Settings>
</Xml1>
Actually from this file i want to delete the Four File1 nodeswhich has the values "a,b,c,d" and then i want to add a node,
实际上,我想从这个文件中删除四个值为“a、b、c、d”的File1 节点,然后我想添加一个节点,
<Setting name="File1" value="e" />
How can i do this.?
我怎样才能做到这一点。?
采纳答案by Fredrik M?rk
It may be easier to use XPath to locate the nodes that you wish to delete. This stackoverflow threadmight give you some ideas.
使用 XPath 定位要删除的节点可能更容易。这个 stackoverflow 线程可能会给你一些想法。
In your case you will find the four nodes that you want using this expression:
在您的情况下,您将使用此表达式找到所需的四个节点:
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
XmlNodeList nodes = doc.SelectNodes("//Setting[@name='File1']");
回答by PQW
DocumentElement
is the root node of the document so childNodes[1]
doesn't exist in that document. childNodes[0]
would be the <Settings> node
DocumentElement
是文档的根节点,因此childNodes[1]
在该文档中不存在。childNodes[0]
将是 <Settings> 节点
回答by idursun
You can use Linq to XMLto do this:
您可以使用Linq to XML来执行此操作:
XDocument doc = XDocument.Load("input.xml");
var q = from node in doc.Descendants("Setting")
let attr = node.Attribute("name")
where attr != null && attr.Value == "File1"
select node;
q.ToList().ForEach(x => x.Remove());
doc.Save("output.xml");
回答by SyncMaster
Deleting nodes from XML
从 XML 中删除节点
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeList nodes = doc.SelectNodes("//Setting[@name='File1']");
for (int i = nodes.Count - 1; i >= 0; i--)
{
nodes[i].ParentNode.RemoveChild(nodes[i]);
}
doc.Save(path);
Adding attribute to Nodes in XML
将属性添加到 XML 中的节点
XmlDocument originalXml = new XmlDocument();
originalXml.Load(path);
XmlNode menu = originalXml.SelectSingleNode("//Settings");
XmlNode newSub = originalXml.CreateNode(XmlNodeType.Element, "Setting", null);
XmlAttribute xa = originalXml.CreateAttribute("name");
xa.Value = "qwerty";
XmlAttribute xb = originalXml.CreateAttribute("value");
xb.Value = "555";
newSub.Attributes.Append(xa);
newSub.Attributes.Append(xb);
menu.AppendChild(newSub);
originalXml.Save(path);