Questions can have more than one answer. Answers can lead to another question. When the Answer's NextQuestionId="0" then there will be no more questions nested underneath.
Questions and Answers have the following attributes:
I think I need to create a class (QuestionAndAnswers) to model Questions which contains a list Answers, but then the Answers could then contain another QuestionAndAnswers class. I need to loop through infinite levels i.e. without having something like the following in the code.
For each
For each
For each
Any thoughts would be greatly appreciated. I am not sure if I am over complicating things or whether I could do the same with simple Do...While or for each loops.
Create classes as per your requirement and then deserilize the XML to create object of the class ..
[Serializable]
[XmlElement("Question")]
public class Question
{
[XmlElement("Answer")]
public List<Answer> Answers { get; set; }
[XmlElement("QuestionId")]
public int QuestionId { get; set; }
[XmlElement("ParentQuestionId")]
public int ParentQuestionId { get; set; }
public Question()
{
Answers = new List<Answer>();
}
}
[Serializable]
[XmlElement("Answer")]
public class Answer
{
[XmlElement("AnswerId")]
public int AnswerId { get; set; }
[XmlAttribute("AnswerId")]
public int NextQuestionId { get; set; }
}
Somnath Mali
.NET Developer , Pune INDIA.
Please Mark As Answer If my reply helped you.
Question :Whats your name|| Answer :Xyz
Question :Whats Xyz|| Answer :Its a Girlz Name
Question :What the prefix, MS. or MRS.|| Answer :Its Ms.
Question :Where do you live|| Answer :India
Please mark this post as Answer if it is of help to you!
I would love to change the world, but they wont give me the source code.
cmsesan1
Member
469 Points
188 Posts
Looping through nested XML
Apr 26, 2012 11:06 AM|LINK
I have some XML like this
Question Answer Question Answer Question Answer NextQuestionId="0" Answer Question Answer Answer Question Answer Question Answer Question Answerand am not sure how to loop through it.
Questions can have more than one answer. Answers can lead to another question. When the Answer's NextQuestionId="0" then there will be no more questions nested underneath.
Questions and Answers have the following attributes:
Question QuestionId, ParentQuestionId
Answer AnswerId, NextQuestionId
I think I need to create a class (QuestionAndAnswers) to model Questions which contains a list Answers, but then the Answers could then contain another QuestionAndAnswers class. I need to loop through infinite levels i.e. without having something like the following in the code.
For each
For each
For each
Any thoughts would be greatly appreciated. I am not sure if I am over complicating things or whether I could do the same with simple Do...While or for each loops.
Here is what I have so far but I am confused!
//Loop through rules and answers //bool blnNest = false; XElement xRules = xeSessionData.Element("Rules"); foreach (var xBaseRule in xRules.Nodes().OfType<XElement>().Where(x => x.Name == "Rule")) { //blnNest = false; int intRuleId = Convert.ToInt32(xBaseRule.Attribute("Id").Value); objValue.AuraSessionRules.Add(new SessionRules() { RuleId = intRuleId }); foreach (var xAnswer in xBaseRule.Nodes().OfType<XElement>().Where(x => x.Name == "Answer")) { objValue.AuraSessionAnswers.Add(new SessionAnswers() { RuleId = intRuleId, AnswerId = Convert.ToInt32(xAnswer.Attribute("Id").Value) }); } //do //{ // blnNest = true; //} while (blnNest == false); }Thank you very very very much for any help.
Ed
somnathmali
Contributor
2816 Points
450 Posts
Re: Looping through nested XML
Apr 26, 2012 11:40 AM|LINK
use XML Serilzatoin API in .NET.
Create classes as per your requirement and then deserilize the XML to create object of the class ..
[Serializable] [XmlElement("Question")] public class Question { [XmlElement("Answer")] public List<Answer> Answers { get; set; } [XmlElement("QuestionId")] public int QuestionId { get; set; } [XmlElement("ParentQuestionId")] public int ParentQuestionId { get; set; } public Question() { Answers = new List<Answer>(); } } [Serializable] [XmlElement("Answer")] public class Answer { [XmlElement("AnswerId")] public int AnswerId { get; set; } [XmlAttribute("AnswerId")] public int NextQuestionId { get; set; } }.NET Developer , Pune INDIA.
Please Mark As Answer If my reply helped you.
cmsesan1
Member
469 Points
188 Posts
Re: Looping through nested XML
Apr 27, 2012 07:57 AM|LINK
I got it working as follows.
//Loop through rules and answers XElement xRules = xeSessionData.Element("Rules"); foreach (var xBaseRule in xRules.Nodes().OfType<XElement>().Where(x => x.Name == "Rule")) { int intRuleId = Convert.ToInt32(xBaseRule.Attribute("Id").Value); objValue.AuraSessionRules.Add(new SessionRules() { RuleId = intRuleId, Seq = Convert.ToInt32(xBaseRule.Attribute("Seq").Value) }); foreach (var xAnswer in xBaseRule.Nodes().OfType<XElement>().Where(x => x.Name == "Answer")) { int intAnswerId = Convert.ToInt32(xAnswer.Attribute("Id").Value); objValue.AuraSessionRulesAnswers.Add(new SessionRulesAnswers() { RuleId = intRuleId, AnswerId = Convert.ToInt32(xAnswer.Attribute("Id").Value), Answer = xAnswer.FirstNode.ToString() }); foreach (var xQandA in xAnswer.DescendantsAndSelf().OfType<XElement>().Where(y => y.Name == "Rule")) { int intRuleIdNested = Convert.ToInt32(xQandA.Attribute("Id").Value); objValue.AuraSessionRules.Add(new SessionRules() { RuleId = intRuleIdNested, Seq = Convert.ToInt32(xQandA.Attribute("Seq").Value) }); foreach (var xA in xQandA.Nodes().OfType<XElement>().Where(z => z.Name == "Answer")) { int intAnswerIdNested = Convert.ToInt32(xA.Attribute("Id").Value); objValue.AuraSessionRulesAnswers.Add(new SessionRulesAnswers() { RuleId = intRuleIdNested, AnswerId = Convert.ToInt32(xA.Attribute("Id").Value), Answer = xA.FirstNode.ToString() }); } } } }Thank you very much for your suggestion Somnath. I will keep that in mind for next time.
Ed
kavita_khand...
Star
9767 Points
1930 Posts
Re: Looping through nested XML
Apr 27, 2012 08:37 AM|LINK
string myQuestions = "<Questions>" + "<Question val='Whats your name'>" + "<Answer val='Xyz'>" + "<Question val='Whats Xyz'>" + "<Answer val='Its a Girlz Name'>" + "<Question val='What the prefix, MS. or MRS.'>" + "<Answer NextQuestionId='0' val='Its Ms.'/>" + "</Question>" + "</Answer>" + "</Question>" + "</Answer>" + "</Question>" + "<Question val='Where do you live'>" + "<Answer NextQuestionId='0' val='India'/>" + "</Question>" + "</Questions>"; XElement xQuestions = XElement.Parse(myQuestions); string questions = string.Empty; string ans = string.Empty; foreach (XElement xe in xQuestions.Descendants()) { if (xe.Name.ToString().ToLower() == "question") { questions = xe.Attribute("val").Value; ans = xe.Element("Answer").Attribute("val").Value; Response.Write("<b>Question :</b>" + questions + "|| <b>Answer :</b>" + ans); Response.Write("<br/>"); } } }This will give you output as -
I would love to change the world, but they wont give me the source code.