473,322 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 11514
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Pikkel | last post by:
i'm looking for a way to replace special characters with characters without accents, cedilles, etc.
9
by: Jiho Han | last post by:
Suppose I have an xml fragment like: <mother> <child name="Bob" sex="M"/> <child name="Jane" sex="F"/> ... </mother> If I wanted to replace the <mother> element to <father> element, what is...
2
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this...
12
by: ~~~ .NET Ed ~~~ | last post by:
Hi, I have a standalone XML file (with the appropriate xml document header) that works fine when I load it using XmlDocument. I can have child elements like this without problems: ...
1
by: soupaman | last post by:
Im trying to output some filtered xml using the xmlTextReader I know the code and commenting needs cleaned up and eventually plan to add the values to a dataset. currently what this is doing is...
1
by: RJN | last post by:
Hi I'm using XMLTextReader to parse the contents of XML. I have issues when the xml content itself has some special characters like & ,> etc. <CompanyName>Johnson & Jhonson</CompanyName>...
16
by: BBM | last post by:
This is so bizarre I hesitate to post it, but I need to get this working. My code looks like this... Private Const cmdTestResourcesSel = "SELECT * FROM TResources" & _ " WHERE Scenario =...
4
by: JimmyW | last post by:
Hello, How characters é and ü should be coded into App.Config file? For Example & must be converted to &amp; but how those two characters é (e with dot) and ü (German u) should be coded? Can...
3
by: Goran Djuranovic | last post by:
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 ("<", "&" ,...
6
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello, I have some XML that is returned to my application from another vendor that I cannot change before it gets to me. I can only alter it after it gets to my application. That being said, I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.