【问题标题】:Display XML with XPath使用 XPath 显示 XML
【发布时间】:2012-09-13 20:08:44
【问题描述】:

我刚开始使用 XML,我有一个表单,用户添加一些信息并保存到 XML 中,这是执行此操作的代码,它工作正常:

     <%@ Page Language="C#" AutoEventWireup="true"CodeBehind="DatosFinancieros.aspx.cs"
Inherits="Tablero.DatosFinancieros" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Import Namespace="System.Xml" %>
<script runat="server">
protected void btnSave_Click(object sender, EventArgs e)
{
    string xmlPath = MapPath("Datos/DatosFinancieros.xml");
    XmlDocument doc = new XmlDocument();
    //Check if the file already exists or not
    if (System.IO.File.Exists(xmlPath))
    {
        doc.Load(xmlPath);
        XmlNode DatosNodo = CreateDatosNodo(doc);
        //Get reference to the Datos node and append the Datos node to it
        XmlNode DatosFinancierosNodo = doc.SelectSingleNode("DatosFinancieros");
        DatosFinancierosNodo.AppendChild(DatosNodo);
        lblResult.Text = "El documento ha sido actualizado";
    }
    else
    {            
        XmlNode declarationNode = doc.CreateXmlDeclaration("1.0", "", "");
        doc.AppendChild(declarationNode);
        XmlNode comment = doc.CreateComment("Este archivo representa un fragmento de lo almacenado");
        doc.AppendChild(comment);            
        XmlNode DatosFinancierosNodo = doc.CreateElement("DatosFinancieros");
        XmlNode DatosNodo = CreateDatosNodo(doc);                        
        //Append the datos node to the DatosFinancieros node            
        DatosFinancierosNodo.AppendChild(DatosNodo);
        //Append the DatosFinancieros node to the document
        doc.AppendChild(DatosFinancierosNodo);
        lblResult.Text = "El documento ha sido creado";
    }
    doc.Save(xmlPath);
}
XmlNode CreateDatosNodo(XmlDocument doc)
{
XmlNode datosNodo = doc.CreateElement("Datos");
// Add Neta Mensual attribute to the Datos Node
XmlAttribute NetaMensualAttribute = doc.CreateAttribute("NetaMensual");
NetaMensualAttribute.Value = txtNetaMensual.Text;
datosNodo.Attributes.Append(NetaMensualAttribute);

XmlAttribute NetaAcumuladoAttribute = doc.CreateAttribute("NetaAcumulado");
NetaAcumuladoAttribute.Value = txtNetaAcumulado.Text;
datosNodo.Attributes.Append(NetaAcumuladoAttribute);

XmlAttribute MensualAttribute = doc.CreateAttribute("Mensual");
MensualAttribute.Value = txtMensual.Text;
datosNodo.Attributes.Append(MensualAttribute);

XmlAttribute AcumuladoAttribute = doc.CreateAttribute("Acumulado");
AcumuladoAttribute.Value = txtAcumulado.Text;
datosNodo.Attributes.Append(AcumuladoAttribute);

XmlAttribute LiquidezAttribute = doc.CreateAttribute("Liquidez");
LiquidezAttribute.Value = txtLiquidez.Text;
datosNodo.Attributes.Append(LiquidezAttribute);

XmlAttribute AccionMensualAttribute = doc.CreateAttribute("AccionMensual");
AccionMensualAttribute.Value = TextAccionMensual.Text;
datosNodo.Attributes.Append(AccionMensualAttribute);

XmlAttribute AccionAcumuladaAttribute = doc.CreateAttribute("AccionAcumulada");
AccionAcumuladaAttribute.Value = TextAccionAcumulada.Text;
datosNodo.Attributes.Append(AccionAcumuladaAttribute);

return datosNodo;
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
    <table>
        <tr>
            <td colspan="2" style="width: 200px; height: 40px">
                <b style="font-size: x-large">Datos Financieros</b>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="width: 174px; height: 40px">
                <b>Utilidad Neta/Capital</b>
            </td>
        </tr>
        <tr>
            <td style="width: 101px; height: 44px">
                Mensual:
            </td>
            <td style="width: 204px; height: 44px">
                <asp:TextBox ID="txtNetaMensual" runat="server" Width="201px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 101px; height: 44px">
                Acumulado:
            </td>
            <td style="width: 204px; height: 44px">
                <asp:TextBox ID="txtNetaAcumulado" runat="server" Width="201px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="width: 174px; height: 40px">
                <b>Utilidad Neta/Ventas</b>
            </td>
        </tr>
        <tr>
            <td style="width: 101px; height: 41px">
                Mensual:
            </td>
            <td style="width: 204px; height: 41px">
                <asp:TextBox ID="txtMensual" runat="server" Width="201px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 101px; height: 41px">
                Acumulado:
            </td>
            <td style="width: 204px; height: 41px">
                <asp:TextBox ID="txtAcumulado" runat="server" Width="201px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 101px; height: 41px">
                Liquidez:
            </td>
            <td style="width: 204px; height: 41px">
                <asp:TextBox ID="txtLiquidez" runat="server" Width="201px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="width: 174px; height: 40px">
                <b>Acción</b>
            </td>
        </tr>
        <tr>
            <td style="width: 101px; height: 41px">
                Mensual:
            </td>
            <td style="width: 204px; height: 41px">
                <asp:TextBox ID="TextAccionMensual" runat="server" Width="201px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 101px; height: 41px">
                Acumulada:
            </td>
            <td style="width: 204px; height: 41px">
                <asp:TextBox ID="TextAccionAcumulada" runat="server" Width="201px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="width: 101px; height: 41px">
                <asp:Button Text="Guardar" runat="server" ID="btnSave" Width="95px" OnClick="btnSave_Click" />
            </td>
        </tr>
        <tr>
            <td colspan="2" style="width: 101px; height: 41px">
                <asp:Label Text="Guardar" runat="server" ID="lblResult" Width="295px" />
            </td>
        </tr>
    </table>
</div>
</form>
</body>
    </html>

现在,我想在此表单上显示该信息,我已完成此操作,正在运行。没有给我任何错误,但没有显示任何内容,只是空白屏幕:

    <body>
<form id="form1" runat="server">
<div>
    <asp:XmlDataSource ID="XmlDataSource1" runat="server" XPath="datos" DataFile="Datos/DatosFinancieros.xml" />
    <asp:FormView ID="FormView1" runat="server" DataSourceID="XmlDataSource1">
        <ItemTemplate>
            <hr />
            <asp:Repeater ID="Repeater1" runat="server" DataSource='<%# XPathSelect("datos") %>'>
                <ItemTemplate>
                    Neto Mensual:
                    <%# XPath("NetaMensual") %>
                    <br />
                    Neta Acumulado:
                    <%# XPath("NetaAcumulado") %>
                    <br />
                    <hr />
                </ItemTemplate>
            </asp:Repeater>
        </ItemTemplate>
    </asp:FormView>
</div>
</form>

任何人都知道我做错了什么,所以信息不显示???很抱歉发了这么长的帖子,但为了更好地了解我在做什么!

【问题讨论】:

  • btnSave 定义在哪里 - 我看到事件处理程序但没有标记?
  • 从下往上第二个!

标签: asp.net xml xpath


【解决方案1】:

查看屏幕的源代码,看看是否出现了 XML。

如果确实出现了,那么问题在于 HTML 是 XML 的一种变体,浏览器将任何带有 XML 样式标签的东西解释为标记而不是信息。

您需要在 标记内对您的 XML 进行 HTML 编码。

<%# Server.HtmlEncode(XPath(xyz)) %>

【讨论】:

  • 我把这个放在哪里了??在我粘贴或直接粘贴到 XML 文件中的第一个代码上??
  • 我注意到您对 Datos 目录使用了相对路径。是不是你的新页面找不到?
  • @Ivelisse 在您的第二个代码部分,在 HTML 中。你说&lt;%# XPath("something") %&gt;的地方,换成&lt;%# Server.HtmlEncode(XPath("something")) %&gt;
  • 给我一个错误:“/”应用程序中的服务器错误。编译错误描述:在编译服务此请求所需的资源期间发生错误。请查看以下特定错误详细信息并适当修改您的源代码。在这一行:
猜你喜欢
  • 2015-01-06
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 2010-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多