how can i remove xml comments from xml file ?C#

已答复 how can i remove xml comments from xml file ?C#

  • 2009年12月23日 17:28
     
     
    hi,

    how to remove xml comments from an existing xml file.
    • 已编辑 ManasMSDN 2009年12月23日 17:29 title
    •  

全部回复

  • 2009年12月23日 17:47
     
      包含代码
    Hi
    Please check this link
    for removing xml comments

    http://bytes.com/topic/c-sharp/answers/629972-how-remove-all-comment-tags-xml-string

    [
    string RemoveComments(string sDoc) {
    XmlDocument xDoc = new XmlDocument();
    xDoc.PreserveWhitespace = false;
    xDoc.LoadXml(sDoc);
    XPathNavigator nav = xDoc.CreateNavigator();
    while (nav.MoveToNext(XPathNodeType.Comment)) {
    nav.DeleteSelf();
    }
    ]


    Happy Codings, RDRaja
  • 2009年12月23日 17:50
     
     已答复 包含代码
    You have to search comments nodes with an XPath query.
    If you just need to remove any comment node, use something like:

    XmlDocument doc = new XmlDocument();
    
    //Code to load Xml data in doc
    
    XmlNodeList allComments = doc.SelectNodes("//comment()");
    
    foreach(XmlNode n in allComments)
    {
    n.ParentNode.RemoveChild(n);
    }
    
    //Save your document
    

    If you want to remove only specific comment nodes, XPath query must be refined accordingly.

    Hope this helps.
    • 已标记为答案 ManasMSDN 2009年12月23日 19:35
    •  
  • 2009年12月23日 18:28
     
     
    hi,


    XmlDocument xDoc = new XmlDocument();
    xDoc.PreserveWhitespace = false;
    xDoc.LoadXml("Books.xml");//--------------------------------------EXception
    XPathNavigator nav = xDoc.CreateNavigator();
    while (nav.MoveToNext(XPathNodeType.Comment))
    {
    nav.DeleteSelf();
    }
    xDoc .Save("Books.xml");

    -----
    or
    -----
    XmlDocument xDoc = new XmlDocument();
    xDoc.PreserveWhitespace = false;
    xDoc.LoadXml("Books.xml");//--------------------------------------EXception
    XmlNodeList allComments = xDoc .SelectNodes("//comment()");
    foreach(XmlNode n in allComments)
    {
    n.ParentNode.RemoveChild(n);
    }

    xDoc .Save("Books.xml");

    for example xml file

    <?

     

    xml version="1.0" encoding="utf-8" ?>
    <!-- hello world-->
    <Book>
    <name>xyz</name>
    <author>ABC</author>
    </Book>

    i want to remove <!-- hello world--> comment

    XmlException was un handled
    "Data at the root level is invalid. Line 1, position 1."
    • 已编辑 ManasMSDN 2009年12月23日 18:43 code
    •  
  • 2009年12月23日 19:08
     
     已答复

    use this code
                XmlDocument xDoc = new XmlDocument();
                //xDoc.PreserveWhitespace = false;
                xDoc.Load("Books.xml");
                XmlNodeList list = xDoc.SelectNodes("//comment()");

                foreach (XmlNode node in list)
                {
                    node.ParentNode.RemoveChild(node);
                }
    textBox1.Text = xDoc.OuterXml.ToString();

    Happy Codings, RDRaja
    • 已标记为答案 ManasMSDN 2009年12月23日 19:27
    •  
  • 2009年12月23日 19:10
     
     
    LoadXml is used to Loads the XML document from the specified string

    if you want to get xml from file, you can use Load Method of XMLDocument

    Happy Codings, RDRaja
  • 2009年12月23日 19:25
    版主
     
      包含代码
    How about the power of regular expressions?

    string xml = @"<?xml version='1.0' encoding='utf-8'?>
    <!-- Test -->
    <Pal>
      <!-- Do
           not
           remove
           -->
      <abc>DEF</abc>
      
    </Pal>";
    
    string pattern = @"(<!--.*?--\>)";
    
    Console.WriteLine( Regex.Replace( xml, pattern, string.Empty, RegexOptions.Singleline ) );
    
    /* output:
     <?xml version='1.0' encoding='utf-8'?>
    
    <Pal>
    
      <abc>DEF</abc>
    
    </Pal> */

    William Wegerson (www.OmegaCoder.Com)
  • 2009年12月23日 19:28
     
     
    Thanks Dharmalinga Raja .