提问人:Tue Sandbæk 提问时间:3/20/2023 最后编辑:Tue Sandbæk 更新时间:3/20/2023 访问量:41
更新或追加子 XML 文件
Update- or Append child XML file
问:
我已经尝试了一切方法,用我的表单中的新数据更新XML文件中的子项。 我有一个表单,如果有 XML 文件,它首先将数据从 XML 读取到表单字段中。 如果没有 XML 文件,它将创建一个新的 XML 文件,并在子项中附加表单中的数据。
我的问题是用新数据更新孩子,我不能这样做,我可以用System.Web.UI.WebControls.TextBox获取文本,也可以获取一个空字段。
然后我想我可以删除子节点并创建新的子节点,但它们只是空的! 有人能告诉我为什么吗?
这是我的XML文件
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<CGTemplateDATA>
<title>Title here</title>
<text>Text here</text>
<reference>Reference here</reference>
</CGTemplateDATA>
这是我的aspx文件
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server">
protected void Save(object sender, EventArgs e)
{
//this.txtReference.Text = DateTime.Now.ToString("hh:mm:ss");
this.txtReference.Text = "Læs mere på tvsyd.dk";
}
protected void Page_Load(object sender, EventArgs e)
{
string xmlPath = MapPath("xml/feed.xml");
XmlDocument xmldoc = new XmlDocument();
if (System.IO.File.Exists(xmlPath))
{
xmldoc.Load(xmlPath);
XmlNode nodeTitle = xmldoc.DocumentElement.SelectSingleNode("title");
txtTitle.Text = nodeTitle.InnerText;
XmlNode nodeText = xmldoc.DocumentElement.SelectSingleNode("text");
txtText.Text = nodeText.InnerText;
XmlNode nodeRef = xmldoc.DocumentElement.SelectSingleNode("reference");
txtReference.Text = nodeRef.InnerText;
lblResult.Text = "Opdater teksten i XML dokumentet";
}
}
</script>
<script runat="server">
protected void btnSave_Click(object sender, EventArgs e)
{
string xmlPath = MapPath("xml/feed.xml");
XmlDocument doc = new XmlDocument();
//Check if the file already exists or not
if (System.IO.File.Exists(xmlPath))
{
//XmlDocument doc = new XmlDocument();
doc.Load(xmlPath);
XmlNode nodeTitle = doc.SelectSingleNode("/CGTemplateDATA/title");
XmlNode nodeText = doc.SelectSingleNode("/CGTemplateDATA/text");
XmlNode nodeReference = doc.SelectSingleNode("/CGTemplateDATA/reference");
XmlNode parent = doc.SelectSingleNode("/CGTemplateDATA");
parent.RemoveChild(nodeTitle);
parent.RemoveChild(nodeText);
parent.RemoveChild(nodeReference);
XmlNode parentNode = CreateNewBookNode(doc);
//Append the cgtemplatedata node to the document
doc.AppendChild(parentNode);
doc.Save(xmlPath);
}
else
{
XmlNode declarationNode = doc.CreateXmlDeclaration("1.0", "utf-8", "yes");
doc.AppendChild(declarationNode);
XmlNode bookNode = CreateBookNode(doc);
//Append the cgtemplatedata node to the document
doc.AppendChild(bookNode);
lblResult.Text = "XML dokumentet er hermed oprettet";
doc.Save(xmlPath);
}
}
XmlNode CreateBookNode(XmlDocument doc)
{
XmlNode bookNode = doc.CreateElement("CGTemplateDATA");
//Add all the children of the book node
XmlNode titleNode = doc.CreateElement("title");
titleNode.InnerText = txtTitle.Text;
bookNode.AppendChild(titleNode);
XmlNode textNode = doc.CreateElement("text");
textNode.InnerText = txtText.Text;
bookNode.AppendChild(textNode);
XmlNode referenceNode = doc.CreateElement("reference");
referenceNode.InnerText = txtReference.Text;
bookNode.AppendChild(referenceNode);
return bookNode;
}
XmlNode CreateNewBookNode(XmlDocument doc)
{
XmlNode parentNode = doc.SelectSingleNode("/CGTemplateDATA");
XmlNode titleNode = doc.CreateElement("title");
titleNode.InnerText = txtTitle.Text;
parentNode.AppendChild(titleNode);
XmlNode textNode = doc.CreateElement("text");
textNode.InnerText = txtText.Text;
parentNode.AppendChild(textNode);
XmlNode referenceNode = doc.CreateElement("reference");
referenceNode.InnerText = txtReference.Text;
parentNode.AppendChild(referenceNode);
return parentNode;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Storke-TV tekst</title>
<link href="css/stylesheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<img alt="" class="style5" src="images/storke_tekst_top.png" /><br />
<br />
<table>
<tr>
<td colspan="2" style="width: 586px">
<b>FORSIGTIG! Denne side giver direkte adgang til at opdatere storke tekst.</b><br />
<br />
1: Udfyld felterne.<br />
2: Tryk på GEM TEKST.<br />
3: Hvis du vil opdatere teksten, så ret den, og tryk på GEM TEKST igen.<br />
</td>
</tr>
<tr>
<td colspan="2" style="width: 586px"> </td>
</tr>
<tr>
<td style="width: 85px; height: 40px">
Overskrift:
</td>
<td style="width: 501px; height: 40px">
<asp:TextBox ID="txtTitle" runat="server" Width="182px" Height="28px" Text="temp" />
</td>
</tr>
<tr>
<td style="width: 85px; height: 41px">
Tekst:
</td>
<td style="width: 501px; height: 152px">
<asp:TextBox ID="txtText" Textmode="Multiline" runat="server" Width="240px" height="150" />
</td>
</tr>
<tr>
<td style="width: 85px; height: 41px">
Henvisning:
</td>
<td style="width: 501px; height: 54px">
<asp:TextBox ID="txtReference" Textmode="Multiline" runat="server" Width="240px" Height="42px" />
< <asp:Button Text="Indsæt web henvisning" runat="server" ID="btnReftxt" Width="142px" Height="24" font-size="12px" OnClick="Save" />
</td>
</tr>
<tr>
<td style="width: 85px; height: 41px">
</td>
<td style="width: 586px; height: 52px">
<asp:Button Text="GEM TEKST" runat="server" ID="btnSave" Width="240px" Height="40" OnClick="btnSave_Click" />
</td>
</tr>
<tr>
<td style="width: 85px; height: 41px">
</td>
<td style="width: 586px; height: 41px">
<asp:Label Text="Gem tekst" runat="server" ID="lblResult" Width="295px"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
答: 暂无答案
评论