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");
Hi guys, I use the (Ctrl+Shift+,) shortcut to comment out selected XML all of the time.
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.
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.
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.
You need to
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);
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