I was hoping to remove all of my XComment from the XElement before sending it to client.
from some reason it doesn't work and the removeMe.Count()=0
any thoughts?
{
// ...
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);
}
the xml file can be somthing like that
<user>
<name> Elen </name>
<userSettings>
<color> blue </color> <!-- the theme color of the page -->
<layout> horizontal </layout> <!-- layout choise -->
<!-- more settings -->
</userSettings>
</user>
You need to use DescendantNodes
instead of Descendants
.
Descendants
returns XElement
instances, so it basically only returns the tags of your XML.DescendantNodes
returns XNode
instances, which includes comments.
doc.DescendantNodes().Where(x => x.NodeType == XmlNodeType.Comment).Remove();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With