我有一个像这样的xml:
<?xml version="1.0" encoding="utf-8"?>
<xs:PersonaRCV xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:Identificacion>201232934</xs:Identificacion>
<xs:Nombre>MAX Luis</xs:Nombre>
<xs:Edad>34</xs:Edad><xs:ExtensionData />
<xs:AbuelosMaternos>
<xs:ExtensionData />
<xs:Abuela>
<xs:ExtensionData />
<xs:Identificacion>000000000</xs:Identificacion>
<xs:Nombre>Juana Porras</xs:Nombre>
</xs:Abuela>
</xs:AbuelosMaternos>
</xs:PersonaRCV>
我希望它在我的aspx页面中看起来像这样:
PersonaRCV:
Identificacion: 201232934
Nombre: MAX Luis
Edad: 34
AbuelosMaternos:
Abuela:
Identificacion: 000000000
Nombre: Juana Porras
我设法使用此函数在页面中缩进整个xml:
Public Function FormatXml(ByVal sUnformattedXml As String) As String
'load unformatted xml into a dom
Dim xd As New XmlDocument()
Try
xd.LoadXml(sUnformattedXml)
'will hold formatted xml
Dim sb As New StringBuilder()
'pumps the formatted xml into the StringBuilder above
Dim sw As New StringWriter(sb)
'does the formatting
Dim xtw As XmlTextWriter = Nothing
Try
'point the xtw at the StringWriter
xtw = New XmlTextWriter(sw)
'we want the output formatted
xtw.Formatting = Formatting.Indented
'get the dom to dump its contents into the xtw
xd.WriteTo(xtw)
Finally
'clean up even if error
If xtw IsNot Nothing Then
xtw.Close()
End If
End Try
Return sb.ToString()
Catch ex As Exception
End Try
'return the formatted xml
Return sUnformattedXml
End Function
我在我的aspx中这样称呼它:
<pre><asp:Label ID="LBXML" runat="server"><%# Server.HtmlEncode(FormatXml(Eval("Respuesta")))%></asp:Label></pre>
最后,使用Firebug看到的代码如下所示:
<xs:PersonaRCV xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:Identificacion>401690010</xs:Identificacion>
<xs:Nombre>MAX FRANCISCO</xs:Nombre>
我试图用jquery替换做一些事情:
var queHay = $("#MostrarEsconder" + myVal + " span").html();
queHay.replace('/<xs:/', '');
但是我无法让它发挥作用
任何人都可以建议一些东西来获得我想要的结果吗?在我的FormatXML函数中使用jquery或代码隐藏!
谢谢!
答案 0 :(得分:0)
只对传递给FormatXML函数的字符串进行编码,而不是对响应进行编码...
<%# FormatXml(Server.HtmlEncode(Eval("Respuesta")))%>
答案 1 :(得分:0)
所以我找到了答案。
使用Jquery我替换了我不需要的所有字符
你可以在这里看到它:
var queHay = $("#MostrarEsconder" + myVal + " span").html();
queHay = queHay.replace(/\<\?xml.*\>/g, '');
queHay = queHay.replace(/xmlns:xsi.*\>/g, '');
queHay = queHay.replace(/\<\/xs:.*\>/g, '');
queHay = queHay.replace(/ExtensionData.*\/\>/g, '');
queHay = queHay.replace(/\>/g, ': ');
queHay = queHay.replace(/\<\;xs\:/g, '');
$("#MostrarEsconder" + myVal + " span").html(queHay);