CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2011
    Posts
    19

    [RESOLVED] XML Comment Error.

    Whenever I'm trying to read the XML, I get this error-

    "An XML comment cannot contain '--', and '-' cannot be the last character"

    Is there anyway, I can remove all comments? The file is very long to remove it manually.

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: XML Comment Error.

    You can read the file as a text file, check each line in a loop and write a new file skipping over the lines or portions of lines you do not want.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Nov 2011
    Posts
    36

    Re: XML Comment Error.

    Here is something I threw together real quick:

    Code:
        Sub Main()
    
            Dim file As String = "D:\file.xml"
            Dim line As String
            Dim lines As New List(Of String)
            Using sr As StreamReader = New StreamReader(file)
                While Not sr.EndOfStream
                    line = sr.ReadLine
                    If line.Trim.StartsWith("<!--") Then Continue While 'Check if the comment is on its own line
                    line = Regex.Replace(line, "(<!--.*?--\>)", String.Empty) 'Removes comments if they are anywhere else on the line
                    lines.Add(line)
                End While
            End Using
            Using sw As StreamWriter = New StreamWriter(file)
                For Each rdyLines As String In lines
                    sw.WriteLine(rdyLines)
                Next
            End Using
    
        End Sub
    Don't forget -
    Imports System.IO
    Imports System.Text.RegularExpressions

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured