How would i iterate of each child of child Nodes and Display its Element Name and attribute Value
As for this issue, you could try to use nested foreach statement or for statement. Here is a sample, you could refer to it:
Code in xml file:
<?xml version="1.0" encoding="utf-8" ?>
<Questions>
<Question id="1" type='SingleChoice'>
<Text>Where is your project primarily located? (select only one)</Text>
<Answers>
<Answer id="1"> In an urbanized area</Answer>
<Answer id="2"> In a metropolitan area outside of core urbanized area </Answer>
<Answer id="3"> In a small city/town</Answer>
<Answer id="4"> In a rural area</Answer>
</Answers>
</Question>
<Question id="2" type='SingleChoice'>
<QuestionType>SingleChoice</QuestionType>
<Text>What neighborhood is the project located in? (select only one)</Text>
<Answers>
<Answer id="1">Primarily a residential area</Answer>
<Answer id="2">Primarily an employment area</Answer>
<Answer id="3">Primarily an industrial/warehousing area</Answer>
</Answers>
</Question>
</Questions>
Code in page (.aspx.cs)
protected void Page_Load(object sender, EventArgs e)
{
XmlDocument xml = new XmlDocument();
xml.Load(Server.MapPath("Questions.xml"));
StringBuilder sb = new StringBuilder();
List<DiagnosticTool> queslist = new List<DiagnosticTool>();
XmlNodeList list = xml.SelectNodes("/Questions/Question");
foreach (XmlNode xn in list)
{
DiagnosticTool dt = new DiagnosticTool();
dt.QuestionId = Convert.ToInt32(xn.Attributes["id"].Value); //Get Question Id
dt.QuestionType = xn.Attributes["type"].Value; //Get Question Type
dt.Text = xn["Text"].InnerText; //Get Text Node
sb.AppendLine(dt.QuestionId.ToString());
sb.AppendLine("\t");
sb.AppendLine(dt.QuestionType);
sb.AppendLine("\t");
sb.AppendLine(dt.Text);
sb.AppendLine("<br />");
List<Answer> answerlist = new List<Answer>();
XmlNodeList list2 = xn.SelectNodes(".//Answers/Answer");
foreach (XmlNode xn2 in list2)
{
Answer an = new Answer();
an.AnswerId = Convert.ToInt32(xn2.Attributes["id"].Value);//Get answer id.
an.Text = xn2.InnerText;//Get answer node.
answerlist.Add(an); //add answer to List
sb.AppendLine(an.AnswerId.ToString());
sb.AppendLine("\t");
sb.AppendLine(an.Text);
sb.AppendLine("<br />");
}
dt.Answers = answerlist;
queslist.Add(dt); // Add question to list
sb.AppendLine("<br />");
}
Response.Write(sb.ToString());
}
Code in class:
public class DiagnosticTool
{
public string Text { get; set; }
public IList<Answer> Answers { get; set; }
public int QuestionId { get; set; }
public string QuestionType { get; set; }
}
public class Answer
{
public int AnswerId { get; set; }
public string Text { get; set; }
}
The output:
1 SingleChoice Where is your project primarily located? (select only one)
1 In an urbanized area
2 In a metropolitan area outside of core urbanized area
3 In a small city/town
4 In a rural area
2 SingleChoice What neighborhood is the project located in? (select only one)
1 Primarily a residential area
2 Primarily an employment area
3 Primarily an industrial/warehousing area
Best regards,
Dillion
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
None
0 Points
1 Post
How would i iterate of each child of child Nodes and Display its Element Name and attribute Value...
Nov 12, 2015 02:07 AM|Deamon_Dagger|LINK
I Have some Sample Data XML,
All-Star
45489 Points
7008 Posts
Microsoft
Re: How would i iterate of each child of child Nodes and Display its Element Name and attribute V...
Nov 12, 2015 04:21 AM|Zhi Lv - MSFT|LINK
Hi Deamon_Dagger,
Welcome to asp.net forum.
As for this issue, you could try to use nested foreach statement or for statement. Here is a sample, you could refer to it:
Code in xml file:
Code in page (.aspx.cs)
Code in class:
The output:
Best regards,
Dillion