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

时间:2022-09-30 19:43:34

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?

当只给出其属性名称(如abc或def)时,如何删除元素?

3 个解决方案

#1


16  

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

#2


1  

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'的节点,你现在可以遍历它并删除它

#3


0  

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

#1


16  

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

#2


1  

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'的节点,你现在可以遍历它并删除它

#3


0  

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