How to ignore comments when reading a XML file into a XmlDocument?
29,633
Solution 1
You can use an XmlReader
with XmlReaderSettings.IgnoreComments
set to true:
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.IgnoreComments = true;
using (XmlReader reader = XmlReader.Create("input.xml", readerSettings))
{
XmlDocument myData = new XmlDocument();
myData.Load(reader);
// etc...
}
(Found from here by searching for XmlDocument ignore comments
)
Solution 2
foreach(XmlNode node in nodeList)
if(node.NodeType != XmlNodeType.Comment)
...
Solution 3
You could simply add filter on your ChildNodes. E.g.
var children = myNode.ChildNodes.Cast<XmlNode>().Where(n => n.NodeType != XmlNodeType.Comment);
Alternatively, you could load the XmlDocument passing in an XmlReader with settings such that XmlReaderSettings.IgnoreComments is true.
using (var file = File.OpenRead("datafile.xml"))
{
var settings = new XmlReaderSettings() { IgnoreComments = true, IgnoreWhitespace = true };
using (var xmlReader = XmlReader.Create(file, settings))
{
var document = new XmlDocument();
document.Load(xmlReader);
// Process document nodes...
}
}
Solution 4
use XmlReaderSettings
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.IgnoreComments = true;
XmlReader reader = XmlReader.Create(sourceFilePath, readerSettings);
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.Load(reader);
Solution 5
Dim pattern As String = String.Empty
Dim xDoc As XmlDocument = New XmlDocument()
xDoc.Load(path)
''Pattern of comments
pattern = "(<!--.*?--\>)"
xDoc.InnerXml = Regex.Replace(xDoc.InnerXml, pattern, String.Empty, RegexOptions.Singleline)
<!--aftr this run ur code-->
Related videos on Youtube
Comments
-
tunnuz almost 2 years
I am trying to read a XML document with C#, I am doing it this way:
XmlDocument myData = new XmlDocument(); myData.Load("datafile.xml");
anyway, I sometimes get comments when reading XmlNode.ChildNodes.
For the benefit of who's experiencing the same requirement, here's how I did it at the end:
/** Validate a file, return a XmlDocument, exclude comments */ private XmlDocument LoadAndValidate( String fileName ) { // Create XML reader settings XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreComments = true; // Exclude comments settings.ProhibitDtd = false; settings.ValidationType = ValidationType.DTD; // Validation // Create reader based on settings XmlReader reader = XmlReader.Create(fileName, settings); try { // Will throw exception if document is invalid XmlDocument document = new XmlDocument(); document.Load(reader); return document; } catch (XmlSchemaException) { return null; } }
Thank you
Tommaso -
Curt Nichols almost 14 yearsAlternatively, hoist the filter out of the loop for readability: foreach(XmlNode node in nodeList.Where(node => node.NodeType != XmlNodeType.Comment)) ...
-
bkovacic about 12 yearsthis excludes <!-- --> comments, but not /* */ which are also valid XML comments (checked with w3c validator)...