Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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");
like image 635
Otto Avatar asked Jun 13 '11 08:06

Otto


People also ask

What is the shortcut to comment a line in XML?

Hi guys, I use the (Ctrl+Shift+,) shortcut to comment out selected XML all of the time.

Does XML allow comments?

Allows notes and other human readable comments to be included within an XML file. XML Parsers should ignore XML comments. Some constrains govern where comments may be placed with an XML file.

How do you comment multiple lines in POM XML?

Commenting out an entire XML block using CTRL+ / comments each line separately instead of creating a block comment. Select the entire block of xml and hit CTRL+/ to comment the entire block. The comment that was present previously now shows up as part of the file and not as a comment anymore.

How do you add a comment in POM XML?

Rules for adding XML commentsDon't use a comment before an XML declaration. You can use a comment anywhere in XML document except within attribute value. Don't nest a comment inside the other comment.


1 Answers

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);
    
like image 139
Samu Lang Avatar answered Nov 03 '22 17:11

Samu Lang