2011-12-05 47 views
9

Trong một tập tin XML như:Cách xóa phần tử xml khỏi tệp?

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

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

Làm thế nào tôi có thể loại bỏ một phần tử khi chỉ tên thuộc tính của nó (như abc hoặc def) được đưa ra?

+0

bạn không cần thẻ winforms. Bản sao hợp pháp http://stackoverflow.com/questions/5004481/linq-remove-element-fron-xml-based-on-attribute-value – Reniuz

Trả lời

15

Bạn có thể thử một cái gì đó như thế này:

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

Điều này là hoàn hảo. Mã của tôi thực sự là chủ yếu giống nhau, nhưng tôi đã gặp khó khăn khi nhận được thuộc tính name. Cảm ơn. – david

+2

Tôi ghét nó khi ai đó đánh tôi với cú đấm :) Khá nhiều mã tôi đã viết. –

+0

@LeslieHanks: cũng xảy ra với tôi - mọi lúc :-) –

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

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

Bây giờ bạn có các nút có tên = 'abc' thuộc tính, bây giờ bạn có thể vòng qua nó và xóa

+0

Tôi không nghĩ rằng LINQ-to-XML có sẵn trong .NET 2.0 .. ... –

+0

bạn nên thêm Thẻ cho .net 2.0 – FosterZ

+0

Tôi đã viết mã này ban đầu, nhưng bây giờ tôi đang sử dụng NET 2. và tôi đang gặp sự cố làm giảm nó. – david

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");` 
Các vấn đề liên quan