Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
this my xml file :

<?xml version="1.0" encoding="utf-8"?>

<Sequence>
  <Inputs>

<!--<Input>readOF</Input> -->
    <Input>readReference</Input>
   
  </Inputs>
 </Step>-->

  </Steps></Sequence>



I want to uncomment this line :
<!--<Input>readOF</Input> -->


Desired output:
<Input>readOF</Input>


What I have tried:

this is my code when I tried to comment xml node :

var document = new XmlDocument();
document.LoadXml(text);
XmlNodeList nodes = document.SelectNodes("/Sequence/Inputs/Input[.='readOF']");
foreach (XmlNode node in nodes)
  node.ParentNode.ReplaceChild(document.CreateComment(node.OuterXml), node);


It works perfectly .
But now what I want is the reverse : uncomment that specific xml node .
Thank you for any help :)
Posted
Updated 23-Oct-18 2:36am
v2

Take a look: To uncomment a commented node in a XML file using C# answered by Chris Taylor



To the best of my knowledge, using XmlDocument, there is no direct way to do this. You will need to do something like the following

1. Get the value of the comment node
2. Create a new XmlNode with the value from step 1
3. Delete the comment node
4. Add the new node from step 2 to the DOM tree

Here is an example with a slightly simplified version of your XML and addressing your quesion in the comments on finding the correct comment node. Note that I query for all comment nodes, obviously you can be more specific and query the portion of the document that you are interested in.
XML
string xml = @"
    <root>
      <!--<reltable toc='no' class='- map/reltable '>
      <relheader class='- map/relheader '>
        <relcolspec type='concept' class='- map/relcolspec '/>      
      </relheader>         
    </reltable> -->

    <!--<reltable toc='no' class='- map '>
      <relheader class='- map/relheader '>
        <relcolspec type='concept' class='- map/relcolspec '/>      
      </relheader>          
    </reltable> -->
  </root>";


C#
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(xml);

XmlNodeList commentedNodes = xdoc.SelectNodes("//comment()");
var commentNode = (from comment in commentedNodes.Cast<XmlNode>()
            where comment.Value.Contains("class='- map '")
            select comment).FirstOrDefault();

if (commentNode != null)
{
  XmlReader nodeReader = XmlReader.Create(new StringReader(commentNode.Value));
  XmlNode newNode = xdoc.ReadNode(nodeReader);
  commentNode.ParentNode.ReplaceChild(newNode, commentNode);
}
 
Share this answer
 
v4
Comments
EM_Y 23-Oct-18 6:55am    
Thank you for your help and time ^^ this is work catching first node with value I want .
I want to uncomment every node with this value ,
exemple :
  <Inputs>

<!--<Input>readOF</Input> -->
<!--<Input>readOF</Input> -->
    <Input>readReference</Input>
   
  </Inputs>


Desired output:
<Input>readOF</Input>
<Input>readOF</Input>


Can you help me plz with that ^^
It looks like you're trying to reverse what you've done in a prior question. If so, I'd do something like the following:

C#
XmlNodeList nodes = document2.SelectNodes("/Sequence/Inputs/comment()[starts-with(., '<Input>')]");
foreach (XmlNode node in nodes)
{
  XmlDocumentFragment fragment = document2.CreateDocumentFragment();
  fragment.InnerXml = node.Value;

  var element = fragment.FirstChild as XmlElement;
  if (element.FirstChild.Value == "readOF")
    node.ParentNode.ReplaceChild(element, node);
}

For reference (for others), prior question can be found at:

Comment specific input XML file code , using WPF .[^]

Some other links to check out:

XPath Syntax[^]

Basic Queries (LINQ to XML) (C#) | Microsoft Docs[^]
 
Share this answer
 
v4
Comments
EM_Y 23-Oct-18 9:02am    
You don't stop helping me ^^ ,
I appreciate your help and time , I'm a beginner in xml , I'm trying to get and do better .
Thank you for your attention .
Eric Lynch 23-Oct-18 9:16am    
No problem, happy to help.

I suggest spending some time familiarizing yourself with XPATH syntax. Its fairly powerful (and not terribly difficult to learn).

If you'll be playing with XML longer term, I'd suggest checking out the newer LINQ to XML APIs (XDocument). I find them easier to use than the original API's (XmlDocument).

I've updated the solution to include links for both.

This site (CodeProject) provides many introductory articles on LINQ itself. Even if you're not planning on playing much with XML, LINQ is well worth your time. Its useful almost any time you're manipulating collections of items.
EM_Y 23-Oct-18 10:14am    
I'll try to check and learn the match I can ^^ .
Thank you.

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