Asp.net 使用XPath显示XML
我刚开始使用XML,我有一个表单,用户在其中添加一些信息,并保存到XML中,这是执行此操作的代码,它工作正常:Asp.net 使用XPath显示XML,asp.net,xml,xpath,Asp.net,Xml,Xpath,我刚开始使用XML,我有一个表单,用户在其中添加一些信息,并保存到XML中,这是执行此操作的代码,它工作正常: <%@ Page Language="C#" AutoEventWireup="true"CodeBehind="DatosFinancieros.aspx.cs" Inherits="Tablero.DatosFinancieros" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transiti
<%@ 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>
受保护的无效btnSave\u单击(对象发送方,事件参数e)
{
字符串xmlPath=MapPath(“Datos/datosfinanceros.xml”);
XmlDocument doc=新的XmlDocument();
//检查文件是否已存在
if(System.IO.File.Exists(xmlPath))
{
文档加载(xmlPath);
XmlNode DatosNodo=CreateDatosNodo(doc);
//获取对Datos节点的引用并将Datos节点附加到该节点
XmlNode DatosFinancierosNodo=doc.SelectSingleNode(“DatosFinancieros”);
DatosFinancialNodo.AppendChild(DatosNodo);
lblResult.Text=“El documento ha sido implementizado”;
}
其他的
{
XmlNode declarationNode=doc.CreateXmlDeclaration(“1.0”、“1.0”、“1.0”);
doc.AppendChild(declarationNode);
XmlNode comment=doc.CreateComment(“Este archivo representa un fragmento de lo almacenado”);
文件附录(注释);
XmlNode DatosFinancialSnodo=doc.CreateElement(“DatosFinancialOS”);
XmlNode DatosNodo=CreateDatosNodo(doc);
//将datos节点附加到DatosFinancieros节点
DatosFinancialNodo.AppendChild(DatosNodo);
//将DatosFinancieros节点附加到文档
附录Child(DatosFinancialSnodo)文件;
lblResult.Text=“El documento ha sido creado”;
}
保存文档(xmlPath);
}
XmlNode CreatedAtosNodeo(XmlDocument文档)
{
XmlNode datosNodo=doc.CreateElement(“Datos”);
//将Neta Mensual属性添加到Datos节点
XmlAttribute NetaMensualAttribute=doc.CreateAttribute(“NetaMensual”);
NetaMensualAttribute.Value=txtnetamensal.Text;
datosNodo.Attributes.Append(netamensualatAttribute);
XmlAttribute NetacumuladoAttribute=doc.CreateAttribute(“Netacumulado”);
netacumuladoAttribute.Value=txtnetacumulado.Text;
datosNodeo.Attributes.Append(netacumuladoAttribute);
xmldattribute MensualAttribute=doc.CreateAttribute(“Mensual”);
MensualAttribute.Value=txtmensal.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=txtluidez.Text;
datosNodo.Attributes.Append(LiquidezAttribute);
xmldattribute AccionMensualAttribute=doc.CreateAttribute(“AccionMensual”);
AccionMensualAttribute.Value=textacionmensual.Text;
datosNodo.Attributes.Append(AccionMensualAttribute);
XmlAttribute AccionAcumuladaAttribute=doc.CreateAttribute(“AccionAcumulada”);
AccionAccumendaAttribute.Value=textAccionAccumenda.Text;
datosNodo.Attributes.Append(accionacumulatidaattribute);
返回达托斯诺多;
}
达托斯金融家酒店
内塔/资本
量纲:
Acumulado:
内塔/文塔斯酒店
量纲:
Acumulado:
Liquidez:
阿克松
量纲:
Acumulada:
现在,我想显示这个表单上的信息,我制作了这个,正在运行。不给我任何错误,但不显示任何东西,只是空白屏幕:
<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>
Neto Mensual:
Neta Acumulado:
有人知道我做错了什么,所以没有显示信息吗???对不起,这篇文章太长了,但这是为了更好地了解我在做什么 查看屏幕的源代码,看看XML是否出现 如果出现了,那么问题在于HTML是XML的变体,浏览器将任何带有XML样式标记的内容解释为标记而不是信息 您需要在标记中对XML进行HTML编码
哪里定义了btnSave?我看到了事件处理程序,但没有它的标记?从下到上的第二个!我把这个放哪儿了??在我粘贴或直接粘贴到XML文件的第一个代码中,我注意到您使用了Datos目录的相对路径。您的新页面可能找不到它吗?@IVELISE在HTML中的第二个代码部分。当你说的是
时,put反而给了我一个错误:“/”应用程序中的服务器错误。编译错误说明:在编译服务此请求所需的资源时出错。请查看以下特定错误详细信息,并适当修改源代码。他在这条线上:
<%# Server.HtmlEncode(XPath(xyz)) %>