C# Question

Error opening XML file (bad name exception)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

struct goodatribute
{
public string goodname;
public int goodID;
public int price;
public override string ToString()
{
return "goodname:" + goodname + " goodID:" + goodID + " goodprice:"+price;
}
}
class MyApp
{
static void Main()
{

List<goodatribute> good = new List<goodatribute>();
goodatribute TV = new goodatribute();
TV.goodname = "LG";
TV.goodID = 10;
TV.price = 100;
goodatribute car = new goodatribute();
car.goodname = "Ferrari";
car.goodID = 12;
car.price = 100000;
good.Add(TV);
good.Add(car);

XmlTextWriter writer = null;

writer = new XmlTextWriter("orders.xml", System.Text.Encoding.Unicode);
writer.Formatting = Formatting.Indented;</code>
<code>writer.WriteStartDocument();

writer.WriteStartElement("orders");
for (int i = 0; i < 2; ++i)
{
writer.WriteStartElement("order" + Convert.ToString(i + 1));
writer.WriteStartElement("Name");
writer.WriteElementString("Name", good[i].goodname);
writer.WriteEndElement();
writer.WriteStartElement("ID");
writer.WriteElementString("ID", Convert.ToString(good[i].goodID));
writer.WriteEndElement();
writer.WriteStartElement("Price");
writer.WriteElementString("Good price", Convert.ToString(good[i].price));
writer.WriteEndElement();
writer.WriteEndElement();
}
writer.WriteEndDocument();
writer.Close();



XmlDocument doc = new XmlDocument();
doc.Load("orders.xml");**//the problems start from here**
for (int i = 0; i < 2; ++i)
{
XmlNodeList nodes = doc.GetElementsByTagName("order" + Convert.ToString(i+1));
Console.WriteLine(nodes[i]["Name"].InnerText);
Console.WriteLine(nodes[i]["ID"].InnerText);
Console.WriteLine(nodes[i]["Price"].InnerText);
}
}
}


I have an error opening the XML file. The exception description is:


Message=Знак "0", шестнадцатеричное значение 0x30, не может стоять в начале имени., строка 3, позиция 10.


Could the problem be in the nodes' names? The XML file is successfully created, but it can't be opened and shown on the screen.

The application seems to fail on this statement:

doc.Load("orders.xml");


What did I do wrong? Can you help me fix it?

Answer

XML names can't contain spaces. You can see this straight away when you open the XML in a syntax highlighter: the price part of the good price node will be interpreted as an attribute of the good node, but it has no corresponding value. Hence the error message explaining that it was expecting = and instead found >.

To fix this, replace the space in the node's name with a hyphen or other valid XML Name character, as seen in the following examples:

writer.WriteElementString("Good-price", Convert.ToString(good[i].price));
writer.WriteElementString("Good_price", Convert.ToString(good[i].price));
writer.WriteElementString("GoodPrice", Convert.ToString(good[i].price));
writer.WriteElementString("Good.Price", Convert.ToString(good[i].price));
Comments