none
How To ignore commnets in Xmldocument

    Question

  • I know how to ignore comments via XmlReaderSetting and XmlReader.
    But I want's all the comments from my XmlDocument to be ignored.
    How Can I do that ?
    Tuesday, October 07, 2008 10:11 AM

Answers

  • I am able to done it via following Code.[Please let me know if i can do any improvement in this code]

    Code Snippet



     XmlReaderSettings readerSettings = new XmlReaderSettings();
                readerSettings.IgnoreWhitespace = true;
                readerSettings.IgnoreComments = true;
                readerSettings.CheckCharacters = true;
                readerSettings.CloseInput = true;
                readerSettings.IgnoreProcessingInstructions = false;
                readerSettings.ValidationFlags = System.Xml.Schema.XmlSchemaValidationFlags.None;
                readerSettings.ValidationType = ValidationType.None;
                XmlReader reader = XmlReader.Create(sourceFilePath, readerSettings);
                XmlDocument myXmlDoc = new XmlDocument();
               myXmlDoc.Load(reader);
                reader = null;

    Tuesday, October 07, 2008 10:31 AM
  • Hi,

     

    Your code is exactly how it should be done. You probably don't need to set all the properties as most of them have the defaults you want already. I would also suggest using the keyword "using" for the XmlReader creation and around the Load method. Setting the reader to null does release it, but the cleanup will happen whenever GC decides is the best time. Since the reader will keep your file opened (and locked) until the cleanup happens you may run into issue if you try to do something with that file after this code (since the reader is still alive and thus holding the lock on the file). The "using" keyword will call Dispose on the reader on the closing brace of that block and thus ensure that the reader will release the file immediately after it's not needed any more.

     

    Thanks,

     

    Tuesday, October 07, 2008 10:57 AM
    Moderator

All replies

  • I am able to done it via following Code.[Please let me know if i can do any improvement in this code]

    Code Snippet



     XmlReaderSettings readerSettings = new XmlReaderSettings();
                readerSettings.IgnoreWhitespace = true;
                readerSettings.IgnoreComments = true;
                readerSettings.CheckCharacters = true;
                readerSettings.CloseInput = true;
                readerSettings.IgnoreProcessingInstructions = false;
                readerSettings.ValidationFlags = System.Xml.Schema.XmlSchemaValidationFlags.None;
                readerSettings.ValidationType = ValidationType.None;
                XmlReader reader = XmlReader.Create(sourceFilePath, readerSettings);
                XmlDocument myXmlDoc = new XmlDocument();
               myXmlDoc.Load(reader);
                reader = null;

    Tuesday, October 07, 2008 10:31 AM
  • Hi,

     

    Your code is exactly how it should be done. You probably don't need to set all the properties as most of them have the defaults you want already. I would also suggest using the keyword "using" for the XmlReader creation and around the Load method. Setting the reader to null does release it, but the cleanup will happen whenever GC decides is the best time. Since the reader will keep your file opened (and locked) until the cleanup happens you may run into issue if you try to do something with that file after this code (since the reader is still alive and thus holding the lock on the file). The "using" keyword will call Dispose on the reader on the closing brace of that block and thus ensure that the reader will release the file immediately after it's not needed any more.

     

    Thanks,

     

    Tuesday, October 07, 2008 10:57 AM
    Moderator
  • My Modified Code is
    Code Snippet

                XmlDocument xmlDoc = null;

                XmlReaderSettings readerSettings = new XmlReaderSettings();
                readerSettings.IgnoreWhitespace = true;
                readerSettings.IgnoreComments = true;

                using (XmlReader reader = XmlReader.Create(sourceFilePath, readerSettings))
                {
                    xmlDoc = new XmlDocument();
                    xmlDoc.Load(reader);
                }


    Tuesday, October 07, 2008 11:13 AM