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.
kavita_khand...
Star
9767 Points
1931 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.