Solved

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

Posted on 2007-03-23
2
319 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
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now