How to ignore comments when reading a XML file int

2019-02-05 19:55发布

This question already has an answer here:

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

6条回答
地球回转人心会变
2楼-- · 2019-02-05 20:18
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-->
查看更多
欢心
3楼-- · 2019-02-05 20:19
foreach(XmlNode node in nodeList)
  if(node.NodeType != XmlNodeType.Comment)
     ...
查看更多
萌系小妹纸
4楼-- · 2019-02-05 20:25

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)

查看更多
Ridiculous、
5楼-- · 2019-02-05 20:27

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...
    }
}
查看更多
霸刀☆藐视天下
6楼-- · 2019-02-05 20:31

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

查看更多
7楼-- · 2019-02-05 20:35

use XmlReaderSettings

XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.IgnoreComments = true;
XmlReader reader = XmlReader.Create(sourceFilePath, readerSettings);
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.Load(reader);
查看更多
登录 后发表回答