2013-02-05 14 views
6

मैं क्लाइंट को भेजने से पहले XElement से अपने सभी एक्सकॉममेंट को हटाने की उम्मीद कर रहा था।XElement से XComments को कैसे निकालें?

किसी कारण यह काम नहीं करता और removeMe.Count() = 0

कोई विचार से

?

{ 

    // ... 

    myXml = XElement.Load(myPath); 
    var removeMe=myXml.Descendants().Where(x => x.NodeType == XmlNodeType.Comment); 
    removeMe.Count();  // this is 0 , (not what i was expected) 
    removeMe.Remove(); 

    //... 

    string myResponseStr = myXml.ToString(SaveOptions.None); 
    context.Response.ContentType = "text/plain"; 
    context.Response.Write(myResponseStr); 
} 

xml फ़ाइल कि

<user> 
    <name> Elen </name> 

    <userSettings> 
     <color> blue </color>    <!-- the theme color of the page --> 
     <layout> horizontal </layout>  <!-- layout choise --> 

     <!-- more settings --> 

    </userSettings> 

</user> 

उत्तर

9

आप Descendants के बजाय DescendantNodes उपयोग करने की आवश्यकता की तरह somthing हो सकता है।

DescendantsXElement उदाहरण देता है, इसलिए यह मूल रूप से केवल आपके एक्सएमएल के टैग लौटाता है।
DescendantNodesXNode उदाहरण देता है, जिसमें टिप्पणियां शामिल हैं।

doc.DescendantNodes().Where(x => x.NodeType == XmlNodeType.Comment).Remove(); 
+0

धन्यवाद। एक आकर्षण की तरह काम करता है ... –

6

मैं भी DescendantNodes का प्रयोग करेंगे लेकिन Where कॉल के बजाय यह OfType<XComment>() अर्थात doc.DescendantNodes().OfType<XComment>().Remove() उपयोग करने के लिए पर्याप्त होता है।

+0

अतिरिक्त टिप के लिए धन्यवाद। –

संबंधित मुद्दे