简体   繁体   English

如何在vb.net中的XML文件生成中使用像“<”或“&”这样的特殊字符

[英]how to use special characters like '<' or '&' in XML file generation in vb.net

I am passing an array to a function which converts the array to an XML. 我将数组传递给一个函数,该函数将数组转换为XML。 But I am not able to use special characters like "<" or "&" . 但我无法使用"<""&"类的特殊字符。 When I pass these it gives error invalid XML file. 当我传递这些时,它会给出错误无效的XML文件。

Protected Sub drawgraph(ByRef dt As DataTable, ByVal name As String)
    Try

        Dim n(dt.Columns.Count - 3) As String
        Dim r(dt.Columns.Count - 3) As String

        For i As Integer = 1 To dt.Columns.Count - 2
            ***n(i - 1) = dt.Columns(i).ColumnName.ToString.Replace("<", "&lt;")***

            r(i - 1) = (dt.Rows(0).Item(i))
        Next
        ChartBuilder1.BuildLabel(n)
        ChartBuilder1.BuildValues(r, "Age Profile")

        ChartBuilder1.GenerateGraph()
        ChartBuilder1.Visible = True
    Catch ex As Exception
        lbl_msg.Text = ex.Message
    End Try
End Sub

I am replacing "<" with "&lt;" 我用"&lt;"替换"<" "&lt;" but it is not working 但它不起作用

Use HttpServerUtility.HtmlEncode to "escape" the special characters. 使用HttpServerUtility.HtmlEncode来“转义”特殊字符。

"<" becomes &lt; “<”变为&lt;

http://msdn.microsoft.com/en-en/library/w3te6wfz.aspx http://msdn.microsoft.com/en-en/library/w3te6wfz.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM