如何使用XMLTextReader替换属性的特殊字符 [英] How to replace attribute's special characters using XMLTextReader

查看:63
本文介绍了如何使用XMLTextReader替换属性的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我遇到了一个问题,当我的<"时,我的XMLTextReader失败.Read()其中一个属性的值中的字符。我要做的是用合法的东西(& lt;,& amp;)替换非法字符(<","&"等),在我将XML文本发送到SQL Server存储过程之前。目前,我正在使用XMLTextReader和StringWriter来执行此操作,这是失败的代码段:

--------------------- -------------------------------------

使用.XMLTextReaderObj

Do While(.Read()= True)

Select Case .NodeType

Case XmlNodeType.Element

Me。 XMLStringWriterObj.Write("<"& .Name)

如果.IsEmptyElement = True那么

EndTagStr =">< /" &安培; .Name& ">"

如果.HasAttributes = True那么

而.MoveToNextAttribute()

Me.XMLStringWriterObj.Write(" { 0} =''{1}''",。Name,替换(替换(替换(替换(替换(.Value,Chr(38),& amp;"),Chr(60),& ;& lt;"),Chr

(62),"& gt;"),Chr(34),"& quot;"),Chr(39 ),"& quot;"))

结束时

结束如果

Else

EndTagStr =">"

如果.HasAttributes = True那么

而.MoveToNextAttribute()

Me.XMLStringWriterObj.Write(" {0} =''{1}''",。Name,替换(替换(替换(替换(替换(.Value,Chr(38),& amp;"),Chr(60), "& lt;"),Chr

(62),"& gt;"),Chr(34),"& quot;"),Chr( 39),&,))

结束时

结束如果

结束如果

Me.XMLStringWriterObj.Write(EndTagStr)

案例XmlNodeType.Text

Me.XMLStringWriterObj.Write(替换(替换(替换(替换(替换(.Value,Chr(38),& amp;"),Chr(替换) 60),& lt;,Chr(62),& gt;,Chr(34),& quot;,
Chr(39),"& quot;))

Case XmlNodeType.EndElement

Me.XMLStringWriterObj.Write("< /" &安培; .Name& ">")

结束选择

循环

。关闭()



任何建议赞赏

谢谢

Goran

解决方案

< blockquote> Hi Goran,

XmlTextReader不能接受包含保留字的节点。在这个

的情况下,它意味着你试图读取的源Xml文件是无效的。

在写入文本文件之前必须转义保留的字符。可以

你能让我知道源xml文件的来源吗?我们可以通过以下选项解决

问题:


1.使源xml文件有效。

2.使用一个简单的TextReader来读取文件以进行转义。


Kevin Yu

Microsoft在线社区支持


== ================================================ <无线电通信/>
通过电子邮件收到我的帖子通知?请参阅
http://msdn.microsoft .com / subscripti ... ult.aspx#notif

ications。

注意:MSDN托管新闻组支持服务适用于非紧急问题

,可以在1个工作日内收到社区或Microsoft支持工程师的初步回复。请注意,每个跟随

的响应可能需要大约2个工作日作为支持

专业人士与您合作可能需要进一步调查才能达到

最有效的分辨率。该产品不适用于需要紧急,实时或基于电话的交互或复杂的b $ b项目分析和转储分析问题的情况。这种性质的问题最好通过联系

Microsoft客户支持服务(CSS)处理
href =http://msdn.microsoft.com/subscriptions/support/default.aspx\"target =_ blank> http://msdn.microsoft.com/subscripti...t/default.aspx

======================================== ==========


(此帖子按原样提供,不作任何保证,并且不授予

权利。 )


Goran Djuranovic写道:


我遇到了我的XMLTextReader *失败的问题* on /.Read()/当我

有<<"其中一个属性的值中的字符。我试图用b * b b做的是用合法的东西替换非法字符(<<","&"等等)

("在我将XML文本发送到SQL Server

存储过程之前,& lt;","& amp;"等)。



如果在属性值中允许使用文字''<''或文字''&'',那么你b $ b可以直接将所谓的XML发送到SQL服务器。但是你b $ b所拥有的并不是格式良好的XML,因此SQL服务器和Xml(文本)阅读器都不会将它接受为XML。您需要使用非XML工具来修复输入,

任何XML解析器都会拒绝格式不正确的标记。

-


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


嗨Kevin,

XML文件来自界面引擎。它将包含属性

,例如Value ="< 30%"或评论="运行更多&吃得健康另一方

不想格式化它,所以没有选择。


我可以这样做的一种方法是使用正则表达式并替换非法XML

字符在任何双引号括起的字符串中,因为属性

是双引号封闭,但我想知道是否还有其他方法。


你有什么建议,你有什么具体的代码示例吗?

类似的东西?


谢谢很多。

Goran Djuranovic

" Kevin Yu [MSFT]" < v - **** @ online.microsoft.com写信息

新闻:WF ************** @ TK2MSFTNGHUB02.phx.gbl ...


嗨Goran,

XmlTextReader不能接受包含保留字的节点。在这个

的情况下,这意味着您尝试读取的源Xml文件无效。

保留的字符必须为在写入文本文件之前进行转义。可以

你能让我知道源xml文件的来源吗?我们可以通过以下选项解决

问题:


1.使源xml文件有效。

2.使用一个简单的TextReader来读取文件以进行转义。


Kevin Yu

Microsoft在线社区支持


== ================================================ <无线电通信/>
通过电子邮件收到我的帖子通知?请参阅
http://msdn.microsoft .com / subscripti ... ult.aspx#notif

ications。

注意:MSDN托管新闻组支持服务适用于非紧急问题

,可以在1个工作日内收到社区或Microsoft支持工程师的初步回复。请注意,每个跟随

的响应可能需要大约2个工作日作为支持

专业人士与您合作可能需要进一步调查才能达到

最有效的分辨率。该产品不适用于需要紧急,实时或基于电话的交互或复杂的b $ b项目分析和转储分析问题的情况。这种性质的问题最好通过联系

Microsoft客户支持服务(CSS)处理
href =http://msdn.microsoft.com/subscriptions/support/default.aspx\"target =_ blank> http://msdn.microsoft.com/subscripti...t/default.aspx

======================================== ==========


(此帖子按原样提供,不作任何保证,并且不授予

权利。 )



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

解决方案

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.)


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/


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.)



这篇关于如何使用XMLTextReader替换属性的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆