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
XML comments are similar to HTML comments. The comments are added as notes or lines for understanding the purpose of an XML code. Comments can be used to include related links, information, and terms. They are visible only in the source code; not in the XML code. Comments may appear anywhere in XML code.
If all you need to do is view the data in an XML file, you're in luck. Just about every browser can open an XML file. In Chrome, just open a new tab and drag the XML file over. Alternatively, right click on the XML file and hover over "Open with" then click "Chrome".
Syntaxes are much easier than XMLDocument and it requires a minimal amount of code. Also XDocument is mutch faster as XmlDocument. XmlDoucument is an old and dirty solution for query an XML document. Make sure to safe the xml after any change.
XDocument is from the LINQ to XML API, and XmlDocument is the standard DOM-style API for XML. If you know DOM well, and don't want to learn LINQ to XML, go with XmlDocument . If you're new to both, check out this page that compares the two, and pick which one you like the looks of better.
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
)
foreach(XmlNode node in nodeList)
if(node.NodeType != XmlNodeType.Comment)
...
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...
}
}
use XmlReaderSettings
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.IgnoreComments = true;
XmlReader reader = XmlReader.Create(sourceFilePath, readerSettings);
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.Load(reader);
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-->
If you want to use an XmlDocument instead of an XmlReader, you might be better off referring to child nodes by name or using XPath.
Then you don't need to worry about comments that have been added, or other nodes, or if the order has changed.
XmlDocument myData = new XmlDocument();
myData.Load("datafile.xml");
XmlNode DocNode = myData.DocumentElement;
XmlNode Child = DocNode ["SomeChildNode"];
This will select "SomeChildNode", a child of the root element.
The next example will loop through all books in books.xml and print the author. It uses the string property selector and Xpath. It shouldn't be affected by comments etc.
XmlDocument myData = new XmlDocument();
myData.Load("books.xml");
XmlNode DocNode = myData.DocumentElement;
XmlNodeList BookNodeList = DocNode.SelectNodes("./book");
foreach (XmlNode Book in BookNodeList)
{
Console.WriteLine(Book["author"].InnerText);
}
Note, with XPath, you could just as easily search for all book elements in the document, using something like ".//book".
books.xml:
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<catalog>
References:
XmlNode.Item Property (String) hxxp://msdn.microsoft.com/en-us/library/sss31aas.aspx XmlNode.SelectNodes Method (String) http://msdn.microsoft.com/en-us/library/hcebdtae.aspx XmlNode.SelectSingleNode Method (String) http://msdn.microsoft.com/en-us/library/fb63z0tw.aspx
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