简体   繁体   English

如何从文件中删除 xml 元素?

[英]How to remove an xml element from file?

In an XML file such as:在 XML 文件中,例如:

<Snippets>
 <Snippet name="abc">
   <SnippetCode>
   code goes here
   </SnippetCode>
 </Snippet>

 <Snippet name="def">
   <SnippetCode>
   code goes here
   </SnippetCode>
 </Snippet>
</Snippets>

How can I remove an element when only its attribute name (like abc or def ) is given?当仅给出其属性名称(如abcdef )时,如何删除元素?

You could try something like this: 你可以尝试这样的事情:

string xmlInput = @"<Snippets>
 <Snippet name=""abc"">
   <SnippetCode>
   code goes here
   </SnippetCode>
 </Snippet>

 <Snippet name=""def"">
   <SnippetCode>
   code goes here
   </SnippetCode>
 </Snippet>
</Snippets>";

// create the XML, load the contents
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlInput);

// find a node - here the one with name='abc'
XmlNode node = doc.SelectSingleNode("/Snippets/Snippet[@name='abc']");

// if found....
if (node != null)
{
   // get its parent node
   XmlNode parent = node.ParentNode;

   // remove the child node
   parent.RemoveChild(node);

   // verify the new XML structure
   string newXML = doc.OuterXml;

   // save to file or whatever....
   doc.Save(@"C:\temp\new.xml");
}
XDocument doc = XDocument.Load("input.xml");
var q = from node in doc.Descendants("Snippet")
    let attr = node.Attribute("name")
    where attr != null && attr.Value == "abc"
    select node;
q.ToList().ForEach(x => x.Remove());
doc.Save("output.xml");

.Net 2.0 .Net 2.0

XmlDocument doc = new XmlDocument();
doc.Load("input.xml");
XmlNodeList nodes = doc.SelectNodes("//Snippet[@name='abc']");

Now you have the nodes whose attribute name='abc', you can now loop through it and delete 现在你有了属性名称为'abc'的节点,你现在可以遍历它并删除

  XElement xEmp = XElement.Load(@"C://Users//Khulu//Documents//Visual Studio 2012//Projects//AMD//Schedule//ToDo.xml");
            //
            xEmp.Add(
                     new XElement("ToDo",
                      new XElement("Item", item),
                       new XElement("date", date),
                        new XElement("time", time),
                        new XElement("due", due),
                        new XElement("description", description))
            );
            xEmp.Save(@"C://Users//Khulu//Documents//Visual Studio 2012//Projects//AMD//Schedule//ToDo.xml");`  XElement xEmp = XElement.Load(@"C://Users//Khulu//Documents//Visual Studio 2012//Projects//AMD//Schedule//ToDo.xml");
            //
            xEmp.Add(
                     new XElement("ToDo",
                      new XElement("Item", item),
                       new XElement("date", date),
                        new XElement("time", time),
                        new XElement("due", due),
                        new XElement("description", description))
            );
            xEmp.Save(@"C://Users//Khulu//Documents//Visual Studio 2012//Projects//AMD//Schedule//ToDo.xml");`

If you're able to use LINQ to XML, that makes it very easy indeed:如果您能够使用 LINQ 到 XML,那确实非常容易:

var doc = XDocument.Load("input.xml");
var query = doc.Descendants("Snippet")
               .Where(x => (string) x.Attribute("name") == "def");

//  Using extension method
query.Remove();

(The question is tagged .NET 2.0, but in 2022 it seems reasonable to assume most users can use LINQ to XML...) (问题标记为 .NET 2.0,但在 2022 年,假设大多数用户可以使用 LINQ 到 XML 似乎是合理的......)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM