Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Another xml Question 1

Status
Not open for further replies.

Trob70

Programmer
Sep 25, 2012
89
0
6
AU
I am trying to produce this line of code using XmlTextwriter

<item>
<question>Who was named Player of the Match for the first Test in the 2019 Ashes series?</question>
<answer>Nathan Lyon</answer>
<answer correct="true">Steve Smith</answer>
<answer>Matthew Wade</answer>
<answer>Pat Cummins</answer>
</item>

I am having a problem with this line <answer correct="true">Steve Smith</answer>
eg....
writer.WriteStartElement("answer")
writer.WriteAttributeString("correct", "true") etc.....
No success !@!@!@!@

Appreciate any help

Regards Robert

 
Howdy!

No success !@!@!@!@

Could you please elaborate?
What is the outcome, and what exactly does "etc....." do?

Best
MakeitSo

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Sorry I put that confusing comment !!

All I am asking is how to get the following working

I have the program working BUT all the xml text is manually coded by me
I am not using any xml functions. eg xmltextwriter etc
eg
spd = Split(spt(LL + 1), ") ")
TheL = spd(1)
If xyz = TheL Then PrintLine(2, "<answer correct=" & Chr(34) & "true" & Chr(34) & ">" & TheL & "</answer>")
If xyz <> TheL Then PrintLine(2, "<answer>" & TheL & "</answer>")


I am trying to use the .net functions to create the same xml file
rather than doing longhand code

There are 5 quizzes in each xml file The following is the 1st of 5

I am trying to use xmltextwriter to create the following


<quiz number="1" question-label="number" answer-label="letter">
<items>
<item>
<question>Who was named Player of the Match for the first Test in the 2019 Ashes series?</question>
<answer>Nathan Lyon</answer>
<answer correct="true">Steve Smith</answer>
<answer>Matthew Wade</answer>
<answer>Pat Cummins</answer>
</item>
<item>
<question>Michael Collins is best known in association with which of the following events?</question>
<answer>Utegate political scandal in 2009</answer>
<answer>Discovery of penicillin</answer>
<answer>Assassination of John F. Kennedy</answer>
<answer correct="true">1969 Apollo 11 moon landing</answer>
</item>

</items>
</quiz>


I hope I am not asking too much, but really would appreciate the help


Regards Robert
 
Hi Robert,

The writing of the xml itself is pretty straightforward. If the node will have child nodes (attributes are also children), use WriteStartElement.
If you use WriteAttributeString, this applies to the previously written node; i.e. WriteStartElement followed directly by WriteAttributeString will add an attribute to that element.

In order get a properly listing of the questions, I have created a class like this:
Code:
class Questions
{
	public string Question { get; set; }
	public Dictionary<string, bool> Answer { get; set; }
}

So each Question has a dictionary of answers, which are qualified as either true or false.

You can then create an instance of this class, populate it and write to xml. Here an example:
Code:
static void Quiz()
{
	var pathToFile = @"C:\00_Work\quiz1.xml";
	var quests = new List<Questions>
	{
		new Questions
		{
			Question = "Who was named Player of the Match for the first Test in the 2019 Ashes series?",
			Answer = new Dictionary<string, bool>
			{
				{"Nathan Lyon", false},
				{"Steve Smith", true},
				{"Matthew Wade", false},
				{"Pat Cummins", false}
			}
		},
		new Questions
		{
			Question =
				"Michael Collins is best known in association with which of the following events?",
			Answer = new Dictionary<string, bool>
			{
				{"Utegate political scandal in 2009", false},
				{"Discovery of penicillin", false},
				{"Assassination of John F. Kennedy", false},
				{"1969 Apollo 11 moon landing", true}
			}
		},
		//and so on
	};

	using (var writer=new XmlTextWriter(pathToFile,Encoding.UTF8))
	{
		writer.WriteStartDocument(true);
		writer.Formatting = Formatting.Indented;
		writer.WriteStartElement("quiz");
		writer.WriteAttributeString("number", "1");
		writer.WriteAttributeString("question-label", "number");
		writer.WriteAttributeString("answer-label", "letter");
		writer.WriteStartElement("items");

		foreach (var quest in quests)
		{
			writer.WriteStartElement("item");
			writer.WriteElementString("question", quest.Question);
			foreach (var answer in quest.Answer)
			{
				writer.WriteStartElement("answer");
				if(answer.Value==true)
					writer.WriteAttributeString("correct", "true");
				writer.WriteString(answer.Key);
				writer.WriteEndElement(); //answer
			}
			writer.WriteEndElement(); //item
		}
		writer.WriteEndElement(); //items
		writer.WriteEndElement(); //quiz
	}
}

Hope this helps.

Best regards
MakeItSo

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
MakeItSo (Programmer)

Thankyou very much for explaining this different method of
creating xml

I will now look closer at this and try using this method.

Really appreciate the time you have given to helping me.

Don't know why, but it has taken me a while coming to grips with xml !!!!
Must be getting old.. (which I am !!)


Regards


Robert

 
Don't worry Robert. I'm sure you'll get the hang of it.
Just wait until you need to handle namespaces...
Probably best to post in the respective language forums like forum732 or forum1867 then... :p

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top