Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Preserving comments with XmlDocument.Load

Preserving comments with XmlDocument.Load

Scheduled Pinned Locked Moved C#
questionxmltutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LannieK
    wrote on last edited by
    #1

    I am reading an XML file in using XmlDocument. The XML file may contain comments that I want to capture and deal with. When I look at the nodes that XmlDocument returns, though, there are no XmlComment nodes. I can see how to create comments using the CreateComment method, but how do I preserve them when I read them in?

    L 1 Reply Last reply
    0
    • L LannieK

      I am reading an XML file in using XmlDocument. The XML file may contain comments that I want to capture and deal with. When I look at the nodes that XmlDocument returns, though, there are no XmlComment nodes. I can see how to create comments using the CreateComment method, but how do I preserve them when I read them in?

      L Offline
      L Offline
      Lars Niedziolka
      wrote on last edited by
      #2

      Hi Lannie, XmlDocument contains all comments if you load an xml document. But the most access functions ignore comments. But following simple code shows message boxes with both comments:

      /\*
       <test>
         <!-- super tip -->
         <tip>Close a application with the cross in the right top corner of the window.</tip>
         <!-- stupid tip -->
         <tip>1+2=3</tip>
      </test>
      \*/
      string xmlstring = @**"<test><!-- super tip --><tip>Close a application with the cross in the right top corner of the window.</tip><!-- stupid tip --><tip>1+2=3</tip></test>"**;
      XmlDocument doc = new XmlDocument();
      doc.LoadXml(xmlstring);
      XmlNode node = doc.SelectSingleNode("/test");
      for (XmlNode subnode = node.FirstChild; subnode != null; subnode = subnode.NextSibling)
      {
         XmlComment comment = subnode as XmlComment;
         if (comment != null)
           MessageBox.Show(comment.Value);
      }
      

      That the XmlDocument contains all comments you see if you look at the OuterXml property of the XmlDocument or save the document in a file. The Comment direct before a XmlElement you get with element.PreviousSibling as XmlComment. The Comment direct behind a XmlElement you get with element.NextSibling as XmlComment. Hope it's help Niedzi

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups