Seeing ALL Comments Using XmlDocument

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am working on a app which will tailor a template xml file as directed by an end user via a GUI. To alter attribute values that works fine. But now I want to be able to remove some comments in the template. But my code is not even
seeing comments in the root node. Am I doing something wrong or is this a limitation of XmlDocument? Heres my prototype code:
<pre class="prettyprint lang-vb Public Class Form1

Dim XmlDoc As XmlDocument = New XmlDocument()

Dim sr As StreamReader

Dim ExeDirInfo As New DirectoryInfo(Directory.GetCurrentDirectory)


Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load

Dim LookingFor As String
LookingFor = ExeDirInfo.FullName & "" & "testXml.xml"
Dim fi As New FileInfo(LookingFor)
Try
sr = New StreamReader(LookingFor)
Catch ex As Exception
MsgBox(ex.ToString)
End Try

Try
Dim filecontent As String = sr.ReadToEnd
tbxXmlIn.Text = filecontent display input xml
XmlDoc.XmlResolver = Nothing <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< T E M P O R A R Y
XmlDoc.LoadXml(filecontent)
Catch ex As Exception
MsgBox(ex.ToString)
End Try

Dim node As XmlNode
node = XmlDoc.DocumentElement get the root XmlElement
tbxStepping.AppendText("root outerxml: " & node.OuterXml & vbCrLf & "-----------------------" & vbCrLf)
tbxStepping.AppendText("root value: " & node.Value & vbCrLf & "-----------------------" & vbCrLf)

Dim DeleteNodeList As New List(Of XmlNode) for later deletion of some comments and nodes

ProduceComments(XmlDoc) go add GUI provided stuff as comments

For Each node2 As XmlNode In node.ChildNodes
tbxStepping.AppendText("node2 name / outerxml: " & node2.Name & " / " & node2.OuterXml & vbCrLf & "-----------------------" & vbCrLf)
MsgBox("Node2 Name: " & node.Name & " / Childnode Name: " & node2.Name)

If node2.NodeType = XmlNodeType.Comment Then
If node2.InnerText.IndexOf("$Cmt$") > -1 Then
DeleteNodeList.Add(node2)
End If
End If

Next node2

For Each NodeToDelete As XmlNode In DeleteNodeList
node.ParentNode.RemoveChild(node)
Next

Dim sw As New StringWriter
Dim xw As New XmlTextWriter(sw)
XmlDoc.WriteTo(xw)
tbxXmlOut.Text = sw.ToString

End Sub


Sub ProduceComments(ByRef thedoc As XmlDocument)

add the GUI provided stuff as comments to the XML

Dim cmt As XmlComment

Dim strGP As String = "GUI Provided "

cmt = thedoc.CreateComment(strGP & "some added comment")
thedoc.InsertBefore(cmt, thedoc.DocumentElement)

End Sub

End Class
[/code]
And heres the output it produces. The template (i.e. input xml) appears in the left window. What the code is "seeing" as it steps through the template is in the middle window, and what the code generates is in the right window. Youll see that the comments
in the root node are not seen.
<img alt="" src="http://social.msdn.microsoft.com/Forums/getfile/157250
Id appreciate any advice or ideas or sympathy.
Thanks, Bob



View the full article
 
Back
Top