all groups > vb.net > january 2007 >
You're in the

vb.net

group:

How to replace attribute's special characters using XMLTextReader


How to replace attribute's special characters using XMLTextReader Goran Djuranovic
1/20/2007 3:21:56 PM
vb.net: 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() =3D True)
Select Case .NodeType
Case XmlNodeType.Element
Me.XMLStringWriterObj.Write("<" & .Name)
If .IsEmptyElement =3D True Then
EndTagStr =3D "></" & .Name & ">"
If .HasAttributes =3D True Then
While .MoveToNextAttribute()
Me.XMLStringWriterObj.Write(" =
{0}=3D'{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 =3D ">"
If .HasAttributes =3D True Then
While .MoveToNextAttribute()
Me.XMLStringWriterObj.Write(" =
{0}=3D'{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(Replace(Replace(Replace(.Valu=
e, Chr(38), "&amp;"), Chr(60), "&lt;"), Chr(62), "&gt;"), Chr(34), =
"&quot;"),=20
Chr(39), "&apos;"))
Case XmlNodeType.EndElement
Me.XMLStringWriterObj.Write("</" & .Name & ">")
End Select
Loop
.Close()
End With

Any suggestions appreciated
Thanks
Re: How to replace attribute's special characters using XMLTextReader Newbie Coder
1/21/2007 11:58:01 AM
Just use the char codes like you are doing without the &amp; etc

Re: How to replace attribute's special characters using XMLTextReader Goran Djuranovic
1/21/2007 1:31:56 PM
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().=20

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=20
[quoted text, click to view]
Just use the char codes like you are doing without the &amp; etc

Re: How to replace attribute's special characters using XMLTextReader Goran Djuranovic
1/30/2007 12:34:00 PM
More information available in "dotnet.xml" thread, so consider this one =
closed.

Goran Djuranovic

"Goran Djuranovic" <goran.djuranovic@newsgroups.nospam> wrote in =
message news:O4Tc1qYPHHA.1604@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().=20

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=20
[quoted text, click to view]
Just use the char codes like you are doing without the &amp; etc

AddThis Social Bookmark Button