J
Just Me
Any ideas on this. I am trying to loop through an xml document to remove
attributes, but Im having so much trouble, any help is appreciated
//THIS IS THE EXCEPTION ( SEE CODE LINE WHERE FAILURE OCCURS
'//Unexpected XML declaration. The XML declaration must be the first node in
the document, and no white space characters are allowed to appear before it.
Line 13, position 11.
//THE XHTML TEXT WHICH IS BEING LOOOKED AT
<table cellspacing="0" rules="all" border="1" id="dgArticles"
style="font-family:Arial;font-size:8pt;width:762px;border-collapse:collapse;">
<tr style="color:White;background-color:Blue;">
<td> </td><td style="width:0.75cm;">ID</td><td
style="width:7cm;">Title</td><td style="width:13cm;">Summary</td><td
style="width:1cm;">Published</td>
</tr><tr valign="Top">
<td><a href='Articles/Art226/Art226.html'
target=_blank>Open</a></td><td>226</td><td>SQL Server 2005
Permissions</td><td>See this article for a handy reference to the complete
list of permissons on SQL Server 2005 </td><td>28/12/2006</td>
</tr><tr valign="Top">
<td><a href='Articles/Art223/Art223.html'
target=_blank>Open</a></td><td>223</td><td>SQL Schemas In SQL
2005</td><td>Want to know a little more about schemas in SQL Server 2005,
take a look at this quick overview. </td><td>25/12/2006</td>
</tr><tr valign="Top">
<td><a href='Articles/Art224/Art224.html'
target=_blank>Open</a></td><td>224</td><td>SQL Server 2005 - Must_Change
option</td><td>When de-checking Enforce Password Policy, SQL Security
responds with an error and refers to Must_Change being in force. This
article shows you how to reverse this. </td><td>27/12/2006</td>
</tr><tr valign="Top">
<td><a href='Articles/Art220/Art220.html'
target=_blank>Open</a></td><td>220</td><td>Installing Adventureworks
Sample</td><td>If you dont install the samples for Adventureworks first
time, getting them on can be a little tricky. This article explains.
</td><td>23/12/2006</td>
</tr>
</table>
'// THE CODE WHICH PROCESSES THE xhtml
Private Sub useXmlDocButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles useXmlDocButton.Click
GC.Collect()
'Clear message
Me.messageTextBox.Text = String.Empty
Dim xmlString As String
'//Some pre-processing here
xmlString = Me.sourcetextBox.Text.ToLower
'//Remove nbsp
xmlString = Regex.Replace(xmlString, " ", "")
'//Remove any explorer codes
xmlString = Regex.Replace(xmlString, "&[a-zA-Z0-9]*;", "")
'//Remove any unquoted attributes which appear at the end of a tag
xmlString = Regex.Replace(xmlString, " [A-Za-z0-9]*=[A-Za-z0-9_]*>", ">")
'//Remove any unquoted attributes which before end of tag
xmlString = Regex.Replace(xmlString, " [A-Za-z0-9]*=[A-Za-z0-9_]* ", "")
'Finally prepend the cml declaration needed
xmlString = "<?xml version='1.0' encoding='utf-8'?> " & xmlString
Me.sourcetextBox.Text = xmlString
'Get the xml into a stream
Dim stream As New System.IO.MemoryStream
stream.Write((New System.Text.UTF8Encoding).GetBytes(xmlString), 0,
xmlString.Length)
stream.Position = 0
Dim xDoc As New System.Xml.XmlDocument
xDoc.Load(stream)
stream.Position = 0
Dim xreader As New System.Xml.XmlTextReader(stream)
Dim xNode As System.Xml.XmlNode
stream.Position = 0
While xreader.Read()
If xreader.NodeType = Xml.XmlNodeType.Element Then
xNode = xDoc.ReadNode(xreader) '//************* THIS IS WHERE IT FAILS //
xNode.Attributes.RemoveAll()
End If
End While
Dim sr As New System.IO.StreamReader(stream)
stream.Position = 0
targetTextBox.Text = sr.ReadToEnd
sr.Close()
sr.Dispose()
xreader.Close()
stream.Close()
stream.Dispose()
attributes, but Im having so much trouble, any help is appreciated
//THIS IS THE EXCEPTION ( SEE CODE LINE WHERE FAILURE OCCURS
'//Unexpected XML declaration. The XML declaration must be the first node in
the document, and no white space characters are allowed to appear before it.
Line 13, position 11.
//THE XHTML TEXT WHICH IS BEING LOOOKED AT
<table cellspacing="0" rules="all" border="1" id="dgArticles"
style="font-family:Arial;font-size:8pt;width:762px;border-collapse:collapse;">
<tr style="color:White;background-color:Blue;">
<td> </td><td style="width:0.75cm;">ID</td><td
style="width:7cm;">Title</td><td style="width:13cm;">Summary</td><td
style="width:1cm;">Published</td>
</tr><tr valign="Top">
<td><a href='Articles/Art226/Art226.html'
target=_blank>Open</a></td><td>226</td><td>SQL Server 2005
Permissions</td><td>See this article for a handy reference to the complete
list of permissons on SQL Server 2005 </td><td>28/12/2006</td>
</tr><tr valign="Top">
<td><a href='Articles/Art223/Art223.html'
target=_blank>Open</a></td><td>223</td><td>SQL Schemas In SQL
2005</td><td>Want to know a little more about schemas in SQL Server 2005,
take a look at this quick overview. </td><td>25/12/2006</td>
</tr><tr valign="Top">
<td><a href='Articles/Art224/Art224.html'
target=_blank>Open</a></td><td>224</td><td>SQL Server 2005 - Must_Change
option</td><td>When de-checking Enforce Password Policy, SQL Security
responds with an error and refers to Must_Change being in force. This
article shows you how to reverse this. </td><td>27/12/2006</td>
</tr><tr valign="Top">
<td><a href='Articles/Art220/Art220.html'
target=_blank>Open</a></td><td>220</td><td>Installing Adventureworks
Sample</td><td>If you dont install the samples for Adventureworks first
time, getting them on can be a little tricky. This article explains.
</td><td>23/12/2006</td>
</tr>
</table>
'// THE CODE WHICH PROCESSES THE xhtml
Private Sub useXmlDocButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles useXmlDocButton.Click
GC.Collect()
'Clear message
Me.messageTextBox.Text = String.Empty
Dim xmlString As String
'//Some pre-processing here
xmlString = Me.sourcetextBox.Text.ToLower
'//Remove nbsp
xmlString = Regex.Replace(xmlString, " ", "")
'//Remove any explorer codes
xmlString = Regex.Replace(xmlString, "&[a-zA-Z0-9]*;", "")
'//Remove any unquoted attributes which appear at the end of a tag
xmlString = Regex.Replace(xmlString, " [A-Za-z0-9]*=[A-Za-z0-9_]*>", ">")
'//Remove any unquoted attributes which before end of tag
xmlString = Regex.Replace(xmlString, " [A-Za-z0-9]*=[A-Za-z0-9_]* ", "")
'Finally prepend the cml declaration needed
xmlString = "<?xml version='1.0' encoding='utf-8'?> " & xmlString
Me.sourcetextBox.Text = xmlString
'Get the xml into a stream
Dim stream As New System.IO.MemoryStream
stream.Write((New System.Text.UTF8Encoding).GetBytes(xmlString), 0,
xmlString.Length)
stream.Position = 0
Dim xDoc As New System.Xml.XmlDocument
xDoc.Load(stream)
stream.Position = 0
Dim xreader As New System.Xml.XmlTextReader(stream)
Dim xNode As System.Xml.XmlNode
stream.Position = 0
While xreader.Read()
If xreader.NodeType = Xml.XmlNodeType.Element Then
xNode = xDoc.ReadNode(xreader) '//************* THIS IS WHERE IT FAILS //
xNode.Attributes.RemoveAll()
End If
End While
Dim sr As New System.IO.StreamReader(stream)
stream.Position = 0
targetTextBox.Text = sr.ReadToEnd
sr.Close()
sr.Dispose()
xreader.Close()
stream.Close()
stream.Dispose()