Unexpected symbol when manually making an XML file

public string buildXMLData() {

		print ("Creating some XML");

		string xmlString;

		xmlString = "<?xml version="1.0" encoding="utf-8"?>";
		xmlString+= "<player>";
		xmlString+= "<profile>";
		xmlString+= "<playername>Bob</playername>";
		xmlString+= "<hp>3500</hp>";
		xmlString+= "<mp>500</mp>";
		xmlString+= "<level>15</level>";
		xmlString+= "</profile>";
		xmlString+= "<inventory>";
		xmlString+= "<item name="item 1" qty="2" />";
		xmlString+= "<item name="item 2" qty="11" />";
		xmlString+= "<item name="item 3" qty="1" />";
		xmlString+= "<item name="item 4" qty="5" />";
		xmlString+= "</inventory>";
		xmlString+= "</player>";

		return xmlString;

	}

I’m getting errors and I don’t know why.
The errors are:

  • Unexpected symbol `1’
  • Unexpected symbol `item’
    Does anybody know why ?

1 Answer

1

Yo are trying to write the item name with quotes within a string. To put quotes into a string you must use the symbol \ before. Rewrite the item lines like this:

xmlString+= "<item name=\"item 1\" qty=\"2\" />";
xmlString+= "<item name=\"item 2\" qty=\"11\" />";
xmlString+= "<item name=\"item 3\" qty=\"1\" />";
xmlString+= "<item name=\"item 4\" qty=\"5\" />";

There problem is it thinks the word item in name=“item 1” is being used as code instead of being used inside the string itself.

Thank you very much Mr dmg0600 !! It works now haha !

You are welcome! Glad to help