• 0

[C#] - Parsing XML - Easiest approach?


Question

I am looking for the easiest approach to parse this XML up.

I was told to use XML.Linq but I don't have a solid tutorial or understanding of Linq

<?xml version="1.0" encoding="UTF-8"?>
<question id="1">
  <text>What is Canada?</text>
  <choice answer="1">Country</choice>
  <choice answer="0">State</choice>
</question>

Any help appreciated,

I have found some code to apply, but I am only parsing the first "choice" tag with this code.

				var qa = from _qa in XElement.Load(ofd.FileName).Descendants("question				 select new
						 {
							 Text = _qa.Element("text").Value,
							 Choice = _qa.Element("choice")

						 };

Aaron

Edited by AaronMT
Link to comment
https://www.neowin.net/forum/topic/700588-c-parsing-xml-easiest-approach/
Share on other sites

8 answers to this question

Recommended Posts

  • 0

using XLinq, try...

using System.Xml.Linq

XDocument doc = XDocument.Parse("<yourhere&gt;");
XElement node = doc.Element("Question")

var qa = from _qa in XElement.Load(node)
						 select new
						 {
							 Text = _qa.Element("text").Value,
							 Choice = _qa.Element("choice")
						 };

something like that should work, you will have to play with it though

I can have a look for sure tomorrow if you still need a hand, but it's 2am at the moment and I truly can't be bothered :D

  • 0

Yeah I'm toying aroudn with the one I posted, it works except it wont return more than one

&lt;choice&gt;

tag values

XDocument xmlDoc = XDocument.Load(ofd.FileName);

				var qa = from _qa in xmlDoc.Descendants("question")
					 select new
					 {
						 Text = _qa.Element("text").Value,
						 Answer = _qa.Element("choice").Attribute("answer").Value,
						 Choice = _qa.Element("choice").Value
					 };
	foreach (var _qa in qa)
				{
					MessageBox.Show(_qa.Choice);
				}

  • 0

Do people use XmlSerializer's Serialize and Deserialize? Its been a long time since I used it. You would have to make a class to fit your XML structure. A class that has A list<> of Questions (an Object that has a question, a list of choices, and the right answer).

http://msdn.microsoft.com/en-us/library/sy...serializer.aspx

http://www.jonasjohn.de/snippets/csharp/xm...zer-example.htm

  • 0

try readxml with dataset :) this way the datatable name will be "question". this way u wont have to deal with nodes and stuff theres a vast improvement on dataset with xml in .net 2.0 its actually better than using the traditional way i guess.. but im not sure if u can read attributes with it.. never tried

Edited by Rejinderi
  • 0
  Rejinderi said:
try readxml with dataset :) this way the datatable name will be "question". this way u wont have to deal with nodes and stuff theres a vast improvement on dataset with xml in .net 2.0 its actually better than using the traditional way i guess.. but im not sure if u can read attributes with it.. never tried

he is using .net 3.5

to the OP ill try and give this a whirl now... however in answer to your first question, using XLinq is the best approach

  • 0
  BGM said:
he is using .net 3.5

to the OP ill try and give this a whirl now... however in answer to your first question, using XLinq is the best approach

+1. XmlDocument is Pretty similar, but XLinq is definately the latest and greatest. Plus LINQ can be used for Databases, and collections to, so it has many uses.

using System.Xml.Linq

XDocument doc = XDocument.Parse("<yourhere&gt;");
XElement node = doc.Element("Question")

var qa = from _qa in XElement.Load(node)
						 select new
						 {
							 Text = _qa.Element("text").Value,
							 Choices = _qa.Elements("choice")
						 };

Edit - added The above should get you all the choices.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.