如何使用c# [duplicate]从XML文件中删除节点

时间:2021-12-16 09:13:08

Possible Duplicate:
How to remove an XmlNode from XmlNodeList

可能的副本:如何从XmlNodeList删除XmlNode。

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 nodes which has the values "a,b,c,d" and then i want to add a node,

实际上,我想从这个文件中删除4个File1节点它的值是“a,b,c,d”然后我想添加一个节点,

<Setting name="File1" value="e" />

How can i do this.?

我该怎么做呢?

4 个解决方案

#1


6  

It may be easier to use XPath to locate the nodes that you wish to delete. This * thread might give you some ideas.

使用XPath查找希望删除的节点可能更容易。这个*线程可能会给你一些想法。

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']");

#2


23  

You can use Linq to XML to 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");

#3


6  

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);

#4


3  

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是文档的根节点,所以在该文档中不存在子节点[1]。子节点[0]将是 节点

#1


6  

It may be easier to use XPath to locate the nodes that you wish to delete. This * thread might give you some ideas.

使用XPath查找希望删除的节点可能更容易。这个*线程可能会给你一些想法。

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']");

#2


23  

You can use Linq to XML to 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");

#3


6  

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);

#4


3  

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是文档的根节点,所以在该文档中不存在子节点[1]。子节点[0]将是 节点