简体   繁体   中英

How to comment a line of a XML file in C# with System.XML

I need to comment and uncomment the 4th line of this XML file using System.XML properties:

<?xml version="1.0" encoding="utf-8"?>
    <configuration>    
        <system.web>
            <customErrors mode="On" defaultRedirect="som_url_here" />
        </system.web>
    </configuration>

Desired output:

<!-- <customErrors mode="On" defaultRedirect="som_url_here" /> -->

It's possible to achieve this without using a file reader?

The node:

XmlNode xmlNodoCE = docWebConfig.DocumentElement.SelectSingleNode("system.web/customErrors");

You need to

  • load the file into an XmlDocument,
  • retrieve the node you want to comment,
  • create a comment node containing the XML content of your original node,
  • add this comment to the original's parent node just before the original node
  • remove it from its parent,
  • write the XmlDocument to a file (the same one).

     String xmlFileName = "Sample.xml"; // Find the proper path to the XML file String xmlFilePath = this.Server.MapPath(xmlFileName); // Create an XmlDocument System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument(); // Load the XML file in to the document xmlDocument.Load(xmlFilePath); // Get the target node using XPath System.Xml.XmlNode elementToComment = xmlDocument.SelectSingleNode("/configuration/system.web/customErrors"); // Get the XML content of the target node String commentContents = elementToComment.OuterXml; // Create a new comment node // Its contents are the XML content of target node System.Xml.XmlComment commentNode = xmlDocument.CreateComment(commentContents); // Get a reference to the parent of the target node System.Xml.XmlNode parentNode = elementToComment.ParentNode; // Replace the target node with the comment parentNode.ReplaceChild(commentNode, elementToComment); xmlDocument.Save(xmlFilePath);

This should do the trick. It is a console app, but the principle is exactly the same. It does assume a file called "web.config" will be in the same folder as the exe:

using System;
using System.Xml;

namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            var document = new XmlDocument();
            document.Load("web.config");

            var element = document.SelectSingleNode("//compilation[@defaultLanguage = 'c#' and @debug = 'true']");

            var comment = document.CreateComment(element.OuterXml);

            element.ParentNode.ReplaceChild(comment, element);

            document.Save("web.config2");

            var document2 = new XmlDocument();
            document2.Load("web.config2");

            var comment2 = document2.SelectSingleNode("//system.web/comment()");
            var newNode = document2.CreateDocumentFragment();
            newNode.InnerXml = comment2.InnerText;
            comment2.ParentNode.ReplaceChild(newNode, comment2);
            document2.Save("web.config3");

            Console.ReadKey();
        }
    }
}

It's saving to different files to show the progression of the xml, obviously, you'd just want to save back to the original file.

edit: You changed the xml since I wrote the answer, but if you change the xpath then it should work exactly the same.

the problem here is that an XmlDocument is a data structure for a load of XML, it will not read in the comments when it parses the file. you are trying to edit the file itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM