Solved

How to grab an attribute from XML node with code behind code?

Posted on 2007-03-23
2
324 Views
Last Modified: 2013-11-26
I have an xml file I am using to build a true and false question with (see below). There is an attribute named isCorrect in the distractor node that I'd like to grab during the ItemDataBound process, what syntax would I use to access that attribute from the code behind?

Code behind partial sample:
            private void questionRepeater_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
            {
                  RepeaterItem item = e.Item;
                  if( (item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem))
                        {
                        Repeater distractorRepeater = (Repeater) item.FindControl("choicesRepeater");
                        DataRowView drv = (DataRowView) item.DataItem;
                        distractorRepeater.DataSource = drv.CreateChildView("question_distractor");
                        distractorRepeater.DataBind();
                        //Response.Write(" item: " +distractorRepeater.);
                        }// end of if itemtype...            
            }// end of questionRepeater...

XML:
<?xml version="1.0" encoding="utf-8" ?>
<quiz>
      <question>
            <stem>An Apple is a fruit?</stem>
            <distractor1 isCorrect="true">True</distractor1>
            <distractor2>False</distractor2>      
      </question>

      <question>
            <stem>A Bird is a vehicle? </stem>
            <distractor1>True</distractor1>
            <distractor2 isCorrect="true">False</distractor2>      
      </question>            
      <question>
            <stem>A Jetta is an animal? </stem>
            <distractor1>True</distractor1>
            <distractor2 isCorrect="true">False</distractor2>      
      </question>      
</quiz>
0
Comment
Question by:cbittner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 total points
ID: 18785760
     System.Xml.XmlDocument FDoc =
                        new System.Xml.XmlDocument();
                  FDoc.Load(@"e:\temp\test.xml");
                  System.Xml.XmlNodeList FList =
                        FDoc.SelectNodes("/quiz/question/distractor1");
                  for(int i=0;i<FList.Count;i++)
                  {
                        Console.WriteLine(FList[i].Attributes["isCorrect"].Value);
                  }
0
 

Author Comment

by:cbittner
ID: 18792947
Thanks.
0

Featured Post

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question