2011-12-05 18 views
9

जैसे एक XML फ़ाइल में:फ़ाइल से xml तत्व को कैसे निकालें?

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

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

मैं एक तत्व कैसे निकाल सकते हैं जब केवल अपने विशेषता नाम (जैसे abc या def) दिया जाता है?

+0

आपको Winforms टैग की आवश्यकता नहीं है। व्यावहारिक डुप्लिकेट http://stackoverflow.com/questions/5004481/linq-remove-element-fron-xml-based-on-attribute-value – Reniuz

उत्तर

15

आप कुछ इस तरह की कोशिश कर सकते:

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

यह सही है। मेरा कोड वास्तव में वही है, लेकिन मुझे नाम विशेषता प्राप्त करने में कठिनाई थी। धन्यवाद। – david

+2

मुझे इससे नफरत है जब कोई मुझे पंच पर मारता है :) मैंने जो कोड लिखा था, उतना ही बहुत कुछ है। –

+0

@ लेस्लीहैंक्स: मेरे साथ भी होता है - हर समय :-) –

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

नेट 2,0

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

अब आप नोड्स थी जिसकी विशेषता name = 'एबीसी' है, अब आप इसे माध्यम से और पाश हटा सकते हैं

+0

मुझे नहीं लगता कि लिंक-टू-एक्सएमएल .NET 2.0 में उपलब्ध है .. ... –

+0

आपको .net 2.0 – FosterZ

+0

के लिए टैग जोड़ना चाहिए, मैंने शुरुआत में यह कोड लिखा था, लेकिन अब मैं नेट 2 का उपयोग कर रहा हूं और मुझे इसे खराब करने में परेशानी हो रही है। – 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");` 
संबंधित मुद्दे