Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my  xml file  :

<?xml version="1.0" encoding="utf-8"?>
<Sequence>
 <Inputs>
    <Input>readOF</Input>
    <Input>readReference</Input>
  </Inputs>
   </Steps>
</Sequence>



I want to comment this xmlfile line
<Input>readOF</Input>
using wpf/c# programmatically
My code is working only when I have one only Input :

System.Xml.XmlNode elementToComment = xmlDocument.SelectSingleNode("/Sequence/Inputs/Input");


but when I have more than one Input nothing works ,

I tried to identify my element value readOF but I coudnt find the right code format :

System.Xml.XmlNode elementToComment = xmlDocument.SelectSingleNode("/Sequence/Inputs/Input/readOF");


Desired output:

C#
<!--<Input>readOF</Input>-->


What I have tried:

My wpf code :
// Find the proper path to the XML file
              String xmlFilePath = "path\\xmfile.xml";

               // 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("/Sequence/Inputs/Input");

               // 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);
               MessageBox.Show("ok");



How can I define my element Input value in my code .
I wish this is clear for you , waiting for any help .
Posted
Updated 18-Oct-18 3:28am
v8
Comments
Richard MacCutchan 17-Oct-18 8:35am    
What is the question?
EM_Y 17-Oct-18 9:44am    
I want to comment and uncomment specific xml file code or line from wpf application using button action
Richard MacCutchan 17-Oct-18 9:50am    
Yes, you already said that in your original post. So, what is your actual problem?
EM_Y 17-Oct-18 10:52am    
here I found some solution , I updated my Problem , please check it , I'll appreciate any help .
Eric Lynch 17-Oct-18 18:29pm    
First, the XML file in your question is not valid. It has a closing tag, for the Steps element, but no opening tag.

Next, its not entirely clear to me what problem you have encountered. Perhaps, after fixing the XML file in your question, you could (at a minimum) add your expected output. And, if you experienced a problem, your actual output. This would more clearly illustrate the difference between the two (expected and actual).

Finally, you may want to consider using LINQ to XML instead. Personally, I find this interface a little friendlier to use. Here is a link:

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/linq-to-xml-programming-overview

Using LINQ to XML, you'd want to do the following.
C#
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Xml.XPath;

namespace DemoApp
{
   public class Program
   {
     public static void Main(string[] args)
     {
       string text = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<Sequence>
  <Inputs>
    <Input>readOF</Input>
    <Input>readReference</Input>
  </Inputs>
</Sequence>";

       XDocument document = XDocument.Parse(text);
       IEnumerable<XElement> elements = document.XPathSelectElements("/Sequence/Inputs/Input[.='readOF']");

       foreach (var element in elements)
         element.ReplaceWith(new XComment(element.ToString()));

       Console.WriteLine(document.ToString());
    }
  }
}

For comparison, here is the old-style version...
var document2 = new XmlDocument();
document2.LoadXml(text);

XmlNodeList nodes = document2.SelectNodes("/Sequence/Inputs/Input[.='readOF']");
foreach (XmlNode node in nodes)
  node.ParentNode.ReplaceChild(document2.CreateComment(node.OuterXml), node);

For information on XPATH, the syntax used within XPathSelectElements / SelectNodes, see:

XPath Syntax[^]
 
Share this answer
 
v4
Comments
EM_Y 18-Oct-18 9:42am    
Your awesome , thank you ^^ , I found solution but the code I found is changing the comment order to up .
your code comment it and save to same order ,Thank you for help and attention :)
EM_Y 18-Oct-18 9:46am    
Can you please explain to me the difference between your code and mine (My answer bellow) and why my code change the comment Order and remote It on the rest inputs ?

Thank you ^^
Eric Lynch 18-Oct-18 9:48am    
You're code is reversing the order. You march through the children in the order they appear, but then you pre-pend (add to the beginning). I think the AppendChild method would have resulted in the correct order.


Though, pretending humility here, I think my code is prettier :)
Richard MacCutchan 18-Oct-18 10:01am    
Why be humble? That is a cool piece of code.
Eric Lynch 18-Oct-18 10:06am    
Nah, I was only joking, truth is credit goes to MS on this one. They made it easy. I often vent about them, but I think they did a nice job with the XML APIs in C#.
I think you're going to have to parse the file as if it was just a text file. I would consider creating a class that loads the file one line at a time into a List<string> object, and then use linq to drill down into the list.

Your main problem is that you need to handle at least three element definition variants because users are unpredictable:

Everything on a single line:
XML
<mykey>value</mykey>

Multi-line elements:
XML
<mykey>
    value
</mykey>

Elements with an implicit closing tag:
XML
<mykey text="value" />

It would be an interesting programming exercise that could do that, and allow features such as adding/removing elements, moving elements in the file, commenting out elements, and merely adding comments.
 
Share this answer
 
v3
System.Xml.XmlNode inputs = xmlDocument.SelectSingleNode("/Sequence/Inputs");
    foreach (System.Xml.XmlNode child in inputs.ChildNodes)
    {
        if (child.InnerText == "readOF")
        {
            String commentContents = child.OuterXml;
            System.Xml.XmlComment commentNode = xmlDocument.CreateComment(commentContents);
           // inputs.RemoveChild(child);
      //  inputs.PrependChild(commentNode); 
child.ParentNode.ReplaceChild(commentNode, child);              
            break;
        }
    }
 
Share this answer
 
v2
Comments
Eric Lynch 18-Oct-18 9:30am    
Please see solution 2, which presents in the correct order.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900