473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How to remove all comment tags from XML string

I need to remove all comments ( between <!-- and --tags) from XML string.

I tried the following code but comments are still present.
Or is it better to open xml string with a StreamReader, read all the text
inside and remove all the "<!--" and "-->" substrings?

How to remove comments ?
string RemoveComments(string sDoc) {
XmlDocument xDoc = new XmlDocument();
xDoc.PreserveWhitespace = false;
xDoc.LoadXml(sDoc);
XPathNavigator nav = xDoc.CreateNavigator();
while (nav.MoveToNext(XPathNodeType.Comment)) {
nav.DeleteSelf();
}

StringWriter sw = new StringWriter();
XmlTextWriter xtw = new XmlTextWriter(sw);
xtw.IndentChar = ' ';
xtw.Indentation = 2;
xtw.Formatting = Formatting.Indented;

xDoc.WriteContentTo(xtw);
xtw.Close();
sw.Close();
return sw.ToString();
}
Andrus.

Apr 9 '07 #1
1 14265
Try something like this:

Use an XPath expression to get all the comment nodes, then iterate through
to remove them:

XmlNodeList list = xmlDoc.SelectNodes("//comment()");

foreach(XmlNode node in list)
{
node.ParentNode.RemoveChild(node);
}

-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Andrus" wrote:
I need to remove all comments ( between <!-- and --tags) from XML string.

I tried the following code but comments are still present.
Or is it better to open xml string with a StreamReader, read all the text
inside and remove all the "<!--" and "-->" substrings?

How to remove comments ?
string RemoveComments(string sDoc) {
XmlDocument xDoc = new XmlDocument();
xDoc.PreserveWhitespace = false;
xDoc.LoadXml(sDoc);
XPathNavigator nav = xDoc.CreateNavigator();
while (nav.MoveToNext(XPathNodeType.Comment)) {
nav.DeleteSelf();
}

StringWriter sw = new StringWriter();
XmlTextWriter xtw = new XmlTextWriter(sw);
xtw.IndentChar = ' ';
xtw.Indentation = 2;
xtw.Formatting = Formatting.Indented;

xDoc.WriteContentTo(xtw);
xtw.Close();
sw.Close();
return sw.ToString();
}
Andrus.

Apr 9 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Nico Grubert | last post by:
Hello, I want to remove all html tags from a string "content" except <a ....>xxx</a>. My script reads like this: ### import re content = re.sub('<((|\n)*)>', '', content)
1
by: Mitchua | last post by:
I am trying to use HTML::Parser to parse an HTML file, remove all HTML tags (including comments, etc.), replace all ENTITIES (e.g. &amp), and put the result into a variable as a string. I figure...
0
by: faktujaa | last post by:
XML Comment tags (except summary, remarks, param and returns) don't work in my small trial program. The comment tags i want to work are see, seealso, exception, etc. But none of them work. I tried...
2
by: JIM.H. | last post by:
Hello, I have a string and I want to remove only certain string, if exits. Here is an example: “ABC,FCG,XXX,FFA”, I need to remove XXX and keep the rest in the string, how can I do that?...
2
by: Chris Lane | last post by:
Hi, I am using the following comment tag to display code samples in my help document. <code> </code> The problem is all I see is a gray bar and no code sample in my help file. Has anybody...
0
by: peter pilsl | last post by:
For feeding the content of an xml-file to a search-indexer I need to remove all tags and extract the plaintext out of a xml-file. I use the null-xls-stylesheet <?xml version="1.0"?>...
0
by: RolfK | last post by:
Hello All, I'm trying to output an XML node tree in COMMENT tags. Processing is done with XSLT2.0. The output schould be avild xml like this: <root> <e c="some element 1"/> <e c="some...
3
by: CF FAN | last post by:
Hi I have to remove # from a string in coldfusion ..but getting error .can u help me? plzzzzz <cfset narrative="nrr#tive"> <cfoutput>#Replace(narrative, '##',' ', 'ALL')#</cfoutput>
11
by: David | last post by:
Hi All, I am working on a script that is theoreticaly simple but I can not get it to work completely. I am dealing with a page spit out by .NET that leaves empty tags in the markup. I need a...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.