c# - 无法显示编辑值
问题描述
我正在使用 LINQ 编写和读取 XML 数据。阅读部分没问题,但写作部分不起作用。
我的 xml 中的发布数据正在正确加载,但是一旦用户尝试保存新更改,数据仍然保持不变。想知道为什么?
阅读帖子数据:
public partial class EditPost : System.Web.UI.Page
{
private void Page_Load(object sender, EventArgs e)
{
string new_title = newtitle.Text.ToString();
string new_description = update_des.Value.ToString();
string path = "C:\\Users\\user\\Source\\Repos\\FoodBlog\\FoodBlog\\Data\\blog_post.xml";
string postid = Request.QueryString["pid"];
if (postid != null)
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
XDocument xdoc = XDocument.Load(fs);
var post = from p in xdoc.Descendants("post")
select new {
Title = p.Element("title"),
Subtitle = p.Element("subtitle"),
Desc = p.Element("description"),
PID = p.Element("pid")
};
foreach (var ePosts in post) {
if (ePosts.PID.Value == postid) {
newtitle.Text = ePosts.Title.Value;
Subtitle.Value = ePosts.Subtitle.Value;
update_des.Value = ePosts.Desc.Value;
}
}
}
}
}
写帖子数据:
protected void Update_btn_click(object sender, EventArgs e)
{
string postid = Request.QueryString["pid"];
if (postid != null)
{
string path = "C:\\Users\\user\\Source\\Repos\\FoodBlog\\FoodBlog\\Data\\blog_post.xml";
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
XDocument xdoc = XDocument.Load(fs);
var post = from p in xdoc.Descendants("post")
where (string)p.Element("pid") == postid
select new {
Title = p.Element("title"),
Subtitle = p.Element("subtitle"),
Desc = p.Element("description"),
PID = p.Element("pid")
};
foreach (var ePosts in post)
{
if (ePosts.PID.Value == postid)
ePosts.Title.ReplaceWith(newtitle.Text);
ePosts.Subtitle.ReplaceWith(Subtitle.Value);
ePosts.Desc.ReplaceWith(update_des.Value);
}
}
}
XML数据结构:
<Posts>
<post>Something<
<pid>p9119</pid>
<title>Something</title>
<description>Something</description>
<subtitle>Something</subtitle>
<date>Something</date>
<author>Something</author>
</Posts>
解决方案
推荐阅读
- uwp - 在 DataTemplate UWP 中绑定 ComboBoxItem 的 IsSelected 属性
- java - 关于java的各种构造函数和参数
- google-analytics - Google Analytics(分析)测量协议批处理请求无法正常工作
- python - SciKit-Learn 中的 TFIDF Vectorizer 仅返回 5 个结果
- regex - 捕获第三个字符之后的文本
- php - 上传文件时显示“允许上传的最大文件大小:0 字节”
- python - 如何让 Tkinter 使用按钮运行命令并在终端中标记我的输出
- spring-boot - 为什么 Springboot Mockmvc.perform 不处理控制器上的异常?
- excel - 如何在excel单元格中查找特定的重复字母
- oracle - ORACLE APEX - 如何从进程内部访问 APPLICATION_PROCESS_ID?