Populate textboxes with XmlNode.Attributes

Posted by Doug on Stack Overflow See other posts from Stack Overflow or by Doug
Published on 2010-06-06T18:02:12Z Indexed on 2010/06/06 21:02 UTC
Read the original article Hit count: 322

Filed under:
|
|

I have Data.xml:

<?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>

I'd like to be able to edit the attributes of each Slide node via GridView (that has a "Select" column added.) And so far I have:

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;
}

The code for LoadXmlData is just a guess on my part - I'm new to working with xml in this way. I'd like have the user to slected the row from the gridview, then populate a set of text boxes with each slide attributed for updating back to the Data.xml file.

The error I'm getting is Object reference not set to an instance of an object" at the line: titleTextBox.Text = xmlnode.Attributes["@title"].InnerText;

so I'm not reaching the attribute "title" of the slide node. Thanks for any ideas you may have.

© Stack Overflow or respective owner

Related posts about c#

Related posts about Xml