0

我有一个 XML 文档,其格式如下所示;

<root>
<DETAIL Replaces="XYZ" />
<DETAIL Description="Problem:<br><br>CRS750 dumps when panel F is opened<br><br>>>y" />
<DETAIL StepsToReproduce="ABC" />
</root>

使用下面的函数,我可以将上面加载的 XML Doc 转换为所需的格式;

Function ProcessDetail(ByVal xmlText As String) As String
   xtr = New XmlTextReader("GetXMLFileLocation")
        xtr.WhitespaceHandling = WhitespaceHandling.None
        xmlDoc.Load(xtr)
        xtr.Close()

    Dim xmlDoc As New XmlDocument()
    xmlDoc.LoadXml(xmlText)
    Dim sDetail As New StringBuilder()
    sDetail.Append("<DETAIL>")
    For Each detailNode As XmlNode In xmlDoc.SelectNodes("//DETAIL")
        If Not detailNode.Attributes Is Nothing Then
            For Each attribute As XmlAttribute In detailNode.Attributes
 sDetail.Append("<" & attribute.Name.ToUpper & ">" & attribute.Value & "</" &    
 attribute.Name.ToUpper & ">")
            Next
        End If
    Next
    sDetail.Append("</DETAIL>")
    Return sDetail.ToString()
End Function

问题:- 由于我在 DETAIL 下的描述节点有一些“HTML”标签,它给出了以下错误。请帮助我避免这种情况并给出以下输出。

ERROR MESSAGE : '<', hexadecimal value 0x3C, is an invalid attribute character. Line 
1,  position 1326.


DESIRED OUTPUT :
 <root>
 <DETAIL>
 <REPLACES>XYZ</REPLACES> 
 <DESCRIPTION>Problem:<br><br>CRS750 dumps when panel F is opened<br> 
 <br>>>y</DESCRIPTION>
 <STEPSTOREPRODUCE>ABC<STEPSTOREPRODUCE/>
 <root>
4

2 回答 2

0

html-in-xml 将是无效的 XML 文档,需要在生成 xml 的源处进行修复。例如,XML 解析器不可能知道什么应该是 XML 的一部分,什么是简单的 html 标记。例如 xml 应该看起来像

<somexmltag>&lt;p&gt;This is a paragraph with embedded &lt;i&gt;italics&lt;/i&gt;&lt;/p>&gt;</somexmltag>

或使用 CDATA:

<somexmltag><![CDATA[<p>This is a paragraph with embedded <i>italics</i></p>]]></somexmltag>
于 2012-10-01T03:23:51.513 回答
0

使用 < 而不是 <, & 代替 & 和可选的 > 而不是>,还有“” 对于“和/或‘对于’

如果您正在生成(或转义)HTML,请确保在其他字符之前更改和符号!:-)

您不能在属性内使用 CDATA 部分。此外,CDATA 部分有时会出现在 HTML 中,并且它们不会嵌套,如果您不小心,可能会导致潜在的代码损坏甚至安全漏洞。(CDATA 注入攻击)。

于 2012-10-01T07:34:58.953 回答