By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,304 Members | 3,201 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,304 IT Pros & Developers. It's quick & easy.

How to replace attribute's special characters using XMLTextReader

P: n/a
Hi all,
I ran into a problem where my XMLTextReader fails on .Read() when I have "<" character in one of the attribute's values. What I am trying to do is replace illegal characters ("<", "&" , etc.) with legal stuff ("&lt;", "&amp;", etc.), before I send the XML text to a SQL Server stored procedure. Currently, I am using XMLTextReader and StringWriter to do this, here is the piece of code that fails:
----------------------------------------------------------
With .XMLTextReaderObj
Do While (.Read() = True)
Select Case .NodeType
Case XmlNodeType.Element
Me.XMLStringWriterObj.Write("<" & .Name)
If .IsEmptyElement = True Then
EndTagStr = "></" & .Name & ">"
If .HasAttributes = True Then
While .MoveToNextAttribute()
Me.XMLStringWriterObj.Write(" {0}='{1}'", .Name, Replace(Replace(Replace(Replace(Replace(.Value, Chr(38), "&amp;"), Chr(60), "&lt;"), Chr
(62), "&gt;"), Chr(34), "&quot;"), Chr(39), "&apos;"))
End While
End If
Else
EndTagStr = ">"
If .HasAttributes = True Then
While .MoveToNextAttribute()
Me.XMLStringWriterObj.Write(" {0}='{1}'", .Name, Replace(Replace(Replace(Replace(Replace(.Value, Chr(38), "&amp;"), Chr(60), "&lt;"), Chr
(62), "&gt;"), Chr(34), "&quot;"), Chr(39), "&apos;"))
End While
End If
End If
Me.XMLStringWriterObj.Write(EndTagStr)
Case XmlNodeType.Text
Me.XMLStringWriterObj.Write(Replace(Replace(Replac e(Replace(Replace(.Value, Chr(38), "&amp;"), Chr(60), "&lt;"), Chr(62), "&gt;"), Chr(34), "&quot;"),
Chr(39), "&apos;"))
Case XmlNodeType.EndElement
Me.XMLStringWriterObj.Write("</" & .Name & ">")
End Select
Loop
.Close()
End With

Any suggestions appreciated
Thanks
Goran
Jan 20 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Just use the char codes like you are doing without the &amp; etc

Newbie Coder
Jan 21 '07 #2

P: n/a
Hi,
I am already doing that, but the problem is that it fails before I can even change "<" it to "&lt;". It fails on .Read().

It looks like the XMLTextReader cannot even go forward if it finds "<", therefore making it impossible to change "<" to "&lt;".

Any other suggestions?
Thanks
Goran
"Newbie Coder" <ne**********@pleasespamme.comwrote in message news:%2****************@TK2MSFTNGP06.phx.gbl...
Just use the char codes like you are doing without the &amp; etc

Newbie Coder
Jan 21 '07 #3

P: n/a
More information available in "dotnet.xml" thread, so consider this one closed.

Goran Djuranovic

"Goran Djuranovic" <go**************@newsgroups.nospamwrote in message news:O4**************@TK2MSFTNGP05.phx.gbl...
Hi,
I am already doing that, but the problem is that it fails before I can even change "<" it to "&lt;". It fails on .Read().

It looks like the XMLTextReader cannot even go forward if it finds "<", therefore making it impossible to change "<" to "&lt;".

Any other suggestions?
Thanks
Goran
"Newbie Coder" <ne**********@pleasespamme.comwrote in message news:%2****************@TK2MSFTNGP06.phx.gbl...
Just use the char codes like you are doing without the &amp; etc

Newbie Coder
Jan 30 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.