×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Another xml Question

Another xml Question

Another xml Question

(OP)
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

RE: Another xml Question

Howdy!

Quote:

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.

RE: Another xml Question

(OP)
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

RE: Another xml Question

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.

RE: Another xml Question

(OP)
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

RE: Another xml Question

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: C# (C sharp): Microsoft or forum1867: Visual Basic (Microsoft) VB.NET then... blllttt

"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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close