删除xml标签,但保留其名称vb.net jquery

时间:2012-02-22 16:30:27

标签: jquery asp.net xml vb.net

我有一个像这样的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看到的代码如下所示:

&lt;xs:PersonaRCV xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;
    &lt;xs:Identificacion&gt;401690010&lt;/xs:Identificacion&gt;
    &lt;xs:Nombre&gt;MAX FRANCISCO&lt;/xs:Nombre&gt; 

我试图用jquery替换做一些事情:

var queHay = $("#MostrarEsconder" + myVal + " span").html();
queHay.replace('/&lt;xs:/', '');
但是我无法让它发挥作用 任何人都可以建议一些东西来获得我想要的结果吗?在我的FormatXML函数中使用jquery或代码隐藏!

谢谢!

2 个答案:

答案 0 :(得分:0)

只对传递给FormatXML函数的字符串进行编码,而不是对响应进行编码...

<%# FormatXml(Server.HtmlEncode(Eval("Respuesta")))%>

答案 1 :(得分:0)

所以我找到了答案。
使用Jquery我替换了我不需要的所有字符 你可以在这里看到它:

 var queHay = $("#MostrarEsconder" + myVal + " span").html();
 queHay = queHay.replace(/\&lt;\?xml.*\&gt;/g, '');
 queHay = queHay.replace(/xmlns:xsi.*\&gt;/g, '');
 queHay = queHay.replace(/\&lt;\/xs:.*\&gt;/g, '');
 queHay = queHay.replace(/ExtensionData.*\/\&gt;/g, '');
 queHay = queHay.replace(/\&gt;/g, ': ');
 queHay = queHay.replace(/\&lt\;xs\:/g, '');
 $("#MostrarEsconder" + myVal + " span").html(queHay);