我需要从文本框接收输入,以便用新文本更新xml文件。
我有这样的代码脚本:
<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))
{
string Path2xml = MapPath("xml/feed.xml");
XDocument xdoc = XDocument.Load(Path2xml);
string txt2Title = txtTitle.Text;
//xdoc.Element("CGTemplateDATA").Element("title").SetValue(txt2Title);
// or even better option
xdoc.Descendants("title").FirstOrDefault().SetValue(txt2Title);
xdoc.Save(Path2xml);
lblResult.Text = "XML dokumentet er blevet opdateret";
}
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;
}
</script>
它对标题中的文本没有任何作用,也不像我对文本框中的文本所期望的那样:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<CGTemplateDATA>
<title>Title</title>
<text>put text here</text>
<reference>put ref here</reference>
</CGTemplateDATA>
没有显示错误,但它不更新我的字符串?
这是我的webfom:
<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="string"></asp:TextBox>
</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"></asp:TextBox>
</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:TextBox>
< <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"/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
我想不通,但也许答案就在我面前?
1条答案
按热度按时间zynd9foi1#
请尝试以下操作。
步骤1
为
asp:TextBox
添加***text***属性,如下所示:步骤#2
之后,它将工作时,您正在获取的textbox值
作为参考,Microsoft官方文档:TextBox Class