我有data.xml中:(已一个“选择”列加)与XmlNode.Attributes填充文本框
<?xml version="1.0" encoding="utf-8" ?>
<data>
<album>
<slide title="Autum Leaves"
description="Leaves from the fall of 1986"
source="images/Autumn Leaves.jpg"
thumbnail="images/Autumn Leaves_thumb.jpg" />
<slide title="Creek"
description="Creek in Alaska"
source="images/Creek.jpg"
thumbnail="images/Creek_thumb.jpg" />
</album>
</data>
我希望能够通过GridView的编辑每张幻灯片节点的属性到目前为止,我有:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedIndex = GridView1.SelectedIndex;
LoadXmlData(selectedIndex);
}
private void LoadXmlData(int selectedIndex)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(MapPath(@"..\photo_gallery\Data.xml"));
XmlNodeList nodelist = xmldoc.DocumentElement.ChildNodes;
XmlNode xmlnode = nodelist.Item(selectedIndex);
titleTextBox.Text = xmlnode.Attributes["title"].InnerText;
descriptionTextBox.Text = xmlnode.Attributes["description"].InnerText;
sourceTextBox.Text = xmlnode.Attributes["source"].InnerText;
thumbTextBox.Text = xmlnode.Attributes["thumbnail"].InnerText;
}
为LoadXmlData的代码仅仅是我的一个猜测 - 我是新来以这种方式使用XML。我希望让用户从gridview中选出行,然后填充一组文本框,每个幻灯片归为更新回Data.xml文件。 。
我得到的错误是不设置到对象的实例对象引用”的行:titleTextBox.Text = xmlnode.Attributes [ “@标题”]的innerText;
所以我没有达到幻灯片节点的属性“标题”。感谢您的任何想法。
你也可以调用只是'SelectNodes(“//幻灯片”)',如果我没有错 – abatishchev 2010-06-06 20:59:58
@marc_s嘿,你的电子有关xlmdoc.DocumentElement和代码示例的xplanation非常棒。为了正常工作,我稍微改变了它: XmlNode node = nodelist.Item(selectedIndex); (节点属性[“title”]!= null) titleTextBox.Text = node.Attributes [“title”]。InnerText; } 谢谢Marc。 – Doug 2010-06-07 00:22:46
@abatishchev:是的,你可以 - 但它对性能不是很好,它会搜索文档中任何位置的“幻灯片”条目。这可能是也可能不是你正在寻找的.... – 2010-06-07 04:59:32