Link to home
Create AccountLog in
Avatar of bluediesel

asked on

need help reading an XML document using C#

Using the following XML document:- <ModuleQuestions>
- <mt_Module mt_ModuleID="1">
- <mt_Question mt_QuestionID="1">
  <mt_Answer mt_AnswerID="1" />
- <mt_Question mt_QuestionID="2">
  <mt_Answer mt_AnswerID="8" />
- <mt_Question mt_QuestionID="3">
  <mt_Answer mt_AnswerID="10" />
  <mt_Answer mt_AnswerID="12">This is the text block of text where the user can explain why they didn't like it.</mt_Answer>
- <mt_Question mt_QuestionID="4">
  <mt_Answer mt_AnswerID="13">This is the text block where they can leave comments</mt_Answer>

I need to read through it and for every questions (mt_Question) I need to get the answer(s) and ultimately write them to the database.

Getting them to the database I know.  Looping through the XML is where I'm stuck.

I'm using C# and the 1.1 framework.

Any help is greatly appreciated.
Avatar of Babycorn-Starfish
Flag of United States of America image


do you have the XML loaded into an XmlDocument? If you load it into an XmlDocument you should be able to use SelectNodes to pull out the questions one by one and use a similar method to select each node within the question.

Sorry not 1.1 installed at the moment so no code :(
You can use this code.
XmlDocument doc = new XmlDocument();
			XmlNodeList questions = doc.SelectNodes(@"//mt_Question");
			XPathNodeIterator answers = null;
			//XPathNavigator failedNode = null;
			string questionId = string.Empty;
			string answerId = string.Empty;
			string answer = string.Empty;
			foreach (XmlNode question in questions)
				questionId = question.CreateNavigator().SelectSingleNode("@mt_QuestionID").Value;
				Console.WriteLine("QuestionID: " + questionId);
				answers = question.CreateNavigator().Select("mt_Answer");
				if (answers != null)
					while (answers.MoveNext())
						if (answers.Current.SelectSingleNode("@mt_AnswerID") != null)
							answerId = answers.Current.SelectSingleNode("@mt_AnswerID").Value;
						if (answers.Current.SelectSingleNode("text()") != null)
							answer = answers.Current.SelectSingleNode("text()").Value;
						Console.WriteLine(answerId + " " + answer);
				questionId = string.Empty;
				answerId = string.Empty;
				answer = string.Empty;

Open in new window

Try the code I have given in my previous post and let me know if you have any issues.
Avatar of bluediesel


I'm trying it now...I'll let you know how it works.

Thanks for the speedy answer!!
Sorry if I'm missing something obvious...but the code won't compile.  The error I'm getting is:

'System.Xml.XPath.XPathNavigator' does not contain a definition for 'SelectSingleNode'

Ok. I will see.....
OOps my bad. That is not in .Net 1.1. Let me give you the code for 1.1. :P
Avatar of jandromeda
Flag of Sri Lanka image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Any luck with the latest code?
Yes, the questions/answers are working fine.  Now I'm trying to get up to the top so I can get the moduleID.

Thank you so much for your help and the speediness of it!!!
Once again...thank you so much for helping me and for getting to it so quickly!!