469,343 Members | 5,395 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,343 developers. It's quick & easy.

How to replace attribute's special characters using XMLTextReader

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 21 '07 #1
12 11326
Hi Goran,

The XmlTextReader cannot accept nodes that contains reserved words. In this
case, it mean the source Xml file you're trying to read from is invalid.
The reserved chars has to be escaped before writing to a text file. Could
you please let me know where the source xml file come from? We might solve
the problem with the following options:

1. Make the source xml file valid.
2. Use a plain TextReader to read the file for escaping.

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Jan 22 '07 #2
Goran Djuranovic wrote:
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.
If a literal '<' or a literal '&' were allowed in an attribute value
then you could send the alleged XML to SQL server directly. But what you
have is not well-formed XML so neither SQL server nor Xml(Text)Reader
will accept it as XML. You need to use non-XML tools to fix the input,
any XML parser will reject not well-formed markup.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jan 22 '07 #3
Hi Kevin,
The XML file is coming from the interface engine. It will contain attributes
such as Value="<30%" or Comment="Run more & eat healthy". The other side
does not want to well-format it, so no choice there.

One way I can do this is to use regular expressions and replace illegal XML
characters within any double-quote-enclosed string, being that attributes
are double-quote eclosed, but I wanted to know if there is any other way.

What do you suggest and do you have any specific code example that would do
something similar?

Thanks a lot.
Goran Djuranovic
"Kevin Yu [MSFT]" <v-****@online.microsoft.comwrote in message
news:WF**************@TK2MSFTNGHUB02.phx.gbl...
Hi Goran,

The XmlTextReader cannot accept nodes that contains reserved words. In
this
case, it mean the source Xml file you're trying to read from is invalid.
The reserved chars has to be escaped before writing to a text file. Could
you please let me know where the source xml file come from? We might solve
the problem with the following options:

1. Make the source xml file valid.
2. Use a plain TextReader to read the file for escaping.

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Jan 22 '07 #4
Hi Martin,
I kind of new that I would need a non-XML tool or procedure in order to do
this, but I don't want to reinvent the wheel if there is one outthere. :-)
In my response the Kevin (above), I said I can do this by using regular
expressions and replacing illegal XML
characters within any double-quote-enclosed string (being that attributes
are double-quote eclosed in my case), but I wanted to know if there is any
other way.

Any other suggestions on what I should use or how I should approach this?

Thanks a lot.
Goran Djuranovic

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Goran Djuranovic wrote:
>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.

If a literal '<' or a literal '&' were allowed in an attribute value then
you could send the alleged XML to SQL server directly. But what you have
is not well-formed XML so neither SQL server nor Xml(Text)Reader will
accept it as XML. You need to use non-XML tools to fix the input, any XML
parser will reject not well-formed markup.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Jan 22 '07 #5
Goran Djuranovic wrote:
The XML file is coming from the interface engine. It will contain attributes
such as Value="<30%" or Comment="Run more & eat healthy". The other side
does not want to well-format it, so no choice there.
Then what the other side sends is not XML at all. You will have a hard
time trying to fix the mess they send you.
One way I can do this is to use regular expressions and replace illegal XML
characters within any double-quote-enclosed string, being that attributes
are double-quote eclosed, but I wanted to know if there is any other way.
As said, XML tools will not work if someone sends not well-formed
markup. You can try to fix it with regular expressions but unescaped &
and < are not only a problem in attribute values but also in element
content.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jan 22 '07 #6
I understand that, but fortunatelly, our side cares about fixing attribute
mess. In case of any "other" mess, the file is marked as invalid. :-)

Anyway, I am asking for any suggestions to fix the attribute mess a not the
explanations of if the XML file is bad and why it is bad.

Thanks a lot.
Goran Djuranovic
"Martin Honnen" <ma*******@yahoo.dewrote in message
news:e2**************@TK2MSFTNGP02.phx.gbl...
Goran Djuranovic wrote:
>The XML file is coming from the interface engine. It will contain
attributes such as Value="<30%" or Comment="Run more & eat healthy". The
other side does not want to well-format it, so no choice there.

Then what the other side sends is not XML at all. You will have a hard
time trying to fix the mess they send you.
>One way I can do this is to use regular expressions and replace illegal
XML characters within any double-quote-enclosed string, being that
attributes are double-quote eclosed, but I wanted to know if there is any
other way.

As said, XML tools will not work if someone sends not well-formed markup.
You can try to fix it with regular expressions but unescaped & and < are
not only a problem in attribute values but also in element content.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Jan 22 '07 #7
As the file in question is not a well-formed xml document, a xml group like
this is not relevant -- do try better luck at a general text-editing forum.

Cheers,
Dimitre Novatchev

"Goran Djuranovic" <go**************@newsgroups.nospamwrote in message
news:O%***************@TK2MSFTNGP03.phx.gbl...
Hi Martin,
I kind of new that I would need a non-XML tool or procedure in order to do
this, but I don't want to reinvent the wheel if there is one outthere. :-)
In my response the Kevin (above), I said I can do this by using regular
expressions and replacing illegal XML
characters within any double-quote-enclosed string (being that attributes
are double-quote eclosed in my case), but I wanted to know if there is any
other way.

Any other suggestions on what I should use or how I should approach this?

Thanks a lot.
Goran Djuranovic

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>Goran Djuranovic wrote:
>>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.

If a literal '<' or a literal '&' were allowed in an attribute value then
you could send the alleged XML to SQL server directly. But what you have
is not well-formed XML so neither SQL server nor Xml(Text)Reader will
accept it as XML. You need to use non-XML tools to fix the input, any XML
parser will reject not well-formed markup.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


Jan 22 '07 #8
Hi Dimitre,
So what you are saying is that this forum is relevant only for well-formed
XML files???
Ha, ha, ha.... :-) You are a funny man.

Now, if you have any suggestions on how to best solve this problem, please
respond. Suggestions are very appreciated.Otherwise, stand by. :-)
FYI, the same question is also cross-posted on VB.NET forum, but here I got
a better response.

Thanks
Goran Djuranovic
"Dimitre Novatchev" <dn********@somewhere.comwrote in message
news:eS*************@TK2MSFTNGP06.phx.gbl...
As the file in question is not a well-formed xml document, a xml group
like this is not relevant -- do try better luck at a general text-editing
forum.

Cheers,
Dimitre Novatchev

"Goran Djuranovic" <go**************@newsgroups.nospamwrote in message
news:O%***************@TK2MSFTNGP03.phx.gbl...
>Hi Martin,
I kind of new that I would need a non-XML tool or procedure in order to
do this, but I don't want to reinvent the wheel if there is one outthere.
:-) In my response the Kevin (above), I said I can do this by using
regular expressions and replacing illegal XML
characters within any double-quote-enclosed string (being that attributes
are double-quote eclosed in my case), but I wanted to know if there is
any other way.

Any other suggestions on what I should use or how I should approach this?

Thanks a lot.
Goran Djuranovic

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>>Goran Djuranovic wrote:

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.

If a literal '<' or a literal '&' were allowed in an attribute value
then you could send the alleged XML to SQL server directly. But what you
have is not well-formed XML so neither SQL server nor Xml(Text)Reader
will accept it as XML. You need to use non-XML tools to fix the input,
any XML parser will reject not well-formed markup.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/



Jan 23 '07 #9
Hi Goran,

As Martin mentioned, the only way is to use regular expression or some
other techniques like finding char by char to replace these invalid chars
to make the Xml well-formed.

HTH. If anything is unclear, please feel free to let me know.

Kevin Yu
Microsoft Online Community Support
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Jan 24 '07 #10
For a further smart-ass answer, if it isn't well-formed, it isn't XML.
Being well-formed with a predictable basic structure is the basis by which
things like schemas,datatyping, and transorms get built. By using an escape
character without itself being escaped, the API has effectively destroyed
information required to ensure the data's integrity. You can use RegEx (and
I've had to to that with XML coming back from the database that wasn't
deserializable), but you're locking yourself in to the assumption that
because an escape character was used as a literal in one place, what's to
say that a real escape character is not to be treated as a literal? In fact,
I'm not sure of what options you have other than using RegEx, since XSLT
requires XML and hence must have a well-formed source

Jan 24 '07 #11

"Goran Djuranovic" <go**************@newsgroups.nospamwrote in message
news:OF**************@TK2MSFTNGP02.phx.gbl...
Hi Dimitre,
So what you are saying is that this forum is relevant only for well-formed
XML files???
Ha, ha, ha.... :-) You are a funny man.

Now, if you have any suggestions on how to best solve this problem, please
respond. Suggestions are very appreciated.Otherwise, stand by. :-)
FYI, the same question is also cross-posted on VB.NET forum, but here I
got a better response.

Thanks
Goran Djuranovic

Hi Goran,

With the same success you may ask this forum a question about an arbitrary
string...
Ha, ha, ha.... :-)
Yes, one could regard asking such questions in an xml-related group as
funny -- they are general text-editing questions and belong to a more
appropriate -- text-editing group.
Now, if you have any suggestions on how to best solve this problem, please
respond.
Yes, I believe that the best solution to this problem will be found in a
text-editing (not specialised xml) group -- and my suggestion is to ask the
question there.

Cheers,
Dimitre Novatchev
>
"Dimitre Novatchev" <dn********@somewhere.comwrote in message
news:eS*************@TK2MSFTNGP06.phx.gbl...
>As the file in question is not a well-formed xml document, a xml group
like this is not relevant -- do try better luck at a general text-editing
forum.

Cheers,
Dimitre Novatchev

"Goran Djuranovic" <go**************@newsgroups.nospamwrote in message
news:O%***************@TK2MSFTNGP03.phx.gbl...
>>Hi Martin,
I kind of new that I would need a non-XML tool or procedure in order to
do this, but I don't want to reinvent the wheel if there is one
outthere. :-) In my response the Kevin (above), I said I can do this by
using regular expressions and replacing illegal XML
characters within any double-quote-enclosed string (being that
attributes
are double-quote eclosed in my case), but I wanted to know if there is
any other way.

Any other suggestions on what I should use or how I should approach
this?

Thanks a lot.
Goran Djuranovic

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl.. .
Goran Djuranovic wrote:

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.

If a literal '<' or a literal '&' were allowed in an attribute value
then you could send the alleged XML to SQL server directly. But what
you have is not well-formed XML so neither SQL server nor
Xml(Text)Reader will accept it as XML. You need to use non-XML tools to
fix the input, any XML parser will reject not well-formed markup.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/




Jan 24 '07 #12
Dimitre,
Why everyone (including you and me) is saying "well-formed XML", than?

By your definition, it should be either XML or not XML, yet you are using
"well-formed XML" expression. You should probably suggest the W3C drop the
"well-formed" syntax out of their specifications.

Anyway, the RegEx technique has been working well for some time now, so I am
sticking with it. Please consider this thread closed.

Thanks everyone for your suggestions.
Goran Djuranovic
"Dimitre Novatchev" <dn********@somewhere.comwrote in message
news:uq**************@TK2MSFTNGP04.phx.gbl...
>
"Goran Djuranovic" <go**************@newsgroups.nospamwrote in message
news:OF**************@TK2MSFTNGP02.phx.gbl...
>Hi Dimitre,
So what you are saying is that this forum is relevant only for
well-formed XML files???
Ha, ha, ha.... :-) You are a funny man.

Now, if you have any suggestions on how to best solve this problem,
please respond. Suggestions are very appreciated.Otherwise, stand by. :-)
FYI, the same question is also cross-posted on VB.NET forum, but here I
got a better response.

Thanks
Goran Djuranovic


Hi Goran,

With the same success you may ask this forum a question about an arbitrary
string...
>Ha, ha, ha.... :-)

Yes, one could regard asking such questions in an xml-related group as
funny -- they are general text-editing questions and belong to a more
appropriate -- text-editing group.
>Now, if you have any suggestions on how to best solve this problem,
please respond.

Yes, I believe that the best solution to this problem will be found in a
text-editing (not specialised xml) group -- and my suggestion is to ask
the question there.

Cheers,
Dimitre Novatchev
>>
"Dimitre Novatchev" <dn********@somewhere.comwrote in message
news:eS*************@TK2MSFTNGP06.phx.gbl...
>>As the file in question is not a well-formed xml document, a xml group
like this is not relevant -- do try better luck at a general
text-editing forum.

Cheers,
Dimitre Novatchev

"Goran Djuranovic" <go**************@newsgroups.nospamwrote in message
news:O%***************@TK2MSFTNGP03.phx.gbl...
Hi Martin,
I kind of new that I would need a non-XML tool or procedure in order to
do this, but I don't want to reinvent the wheel if there is one
outthere. :-) In my response the Kevin (above), I said I can do this by
using regular expressions and replacing illegal XML
characters within any double-quote-enclosed string (being that
attributes
are double-quote eclosed in my case), but I wanted to know if there is
any other way.

Any other suggestions on what I should use or how I should approach
this?

Thanks a lot.
Goran Djuranovic

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl. ..
Goran Djuranovic wrote:
>
>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.
>
If a literal '<' or a literal '&' were allowed in an attribute value
then you could send the alleged XML to SQL server directly. But what
you have is not well-formed XML so neither SQL server nor
Xml(Text)Reader will accept it as XML. You need to use non-XML tools
to fix the input, any XML parser will reject not well-formed markup.
>
>
--
>
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/




Jan 30 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by ~~~ .NET Ed ~~~ | last post: by
16 posts views Thread by BBM | last post: by
4 posts views Thread by JimmyW | last post: by
6 posts views Thread by =?Utf-8?B?R2Vvcmdl?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.