【发布时间】:2012-09-30 12:17:28
【问题描述】:
<main>
<myself>
<pid>1</pid>
<name>abc</name>
</myself>
<myself>
<pid>2</pid>
<name>efg</name>
</myself>
</main>
那是我的名为 simpan 的 XML 文件。我有两个按钮。下一个和上一个。我想要做的是,当用户单击按钮时,所有信息都将显示在 TextBox 上。搜索节点将基于 pid。
下一步按钮将添加 1 个 pid 值(假设 pid=2),它将搜索具有相同 pid=2 值的节点。它还将显示 pid=2 的名称。 (显示名称=abc)
同样转到上一个按钮,它将减少 1 个 pid 值 (pid=1)。
有人知道怎么做吗?
//------------编辑-------
感谢 L.B,我正在尝试使用他的代码。但是我遇到了错误。
我的代码实现是否正确?
private void previousList_Click(object sender, EventArgs e)
{
pid = 14;
XDocument xDoc = XDocument.Parse("C:\\Users\\HDAdmin\\Documents\\Fatty\\SliceEngine\\SliceEngine\\bin\\Debug\\simpan.xml");
var name = xDoc.Descendants("myself")
.First(m => (int)m.Element("PatientID") == pid)
.Value;
textETA.Text = name;
////////////////////
}
【问题讨论】:
-
将插入符号放在
XDocument.Parse上并按F1,然后阅读文档,尤其是参数说明。 -
使用 XML 字符串调用 XDocument.Parse(),使用带有文件名的 XDocument.Load()。