- Home /
Invalid Encoding Specification Xml
Hi, I want to parse and Xml File. This is my code.
string path = "Assets/Resources/Xml/Level1.xml";
FileStream fsCPAP = null;
XmlDocument xmlDoc = new XmlDocument ();
void Start ()
{
if(File.Exists(path))
{
fsCPAP = new FileStream(path, FileMode.Open, FileAccess.Read);
XmlTextReader rdrXml = new XmlTextReader(fsCPAP);
do
{
switch(rdrXml.NodeType)
{
case XmlNodeType.Text:
print( rdrXml.Value);
break;
}
}
while(rdrXml.Read()); //as longs as read return true
//Once Read() returns false, STOP!@!
}
else return;
}
Xml file
<?xml version = "1.0" encoding = "utf - 8"?>
<levels>
<level>
<Title>level1</Title>
<ItemNumber> 2 </ItemNumber>
</level>
<level>
<Title>level2</Title>
<ItemNumber> 2 </ItemNumber>
</level>
</levels>
The error I got at line XmlTextReader rdrXml = new XmlTextReader(fsCPAP);
XmlException: invalid encoding specification.
Any help ?
Answer by Graham-Dunnett · Jul 09, 2014 at 12:12 PM
Read exception, learn that something doesn't like the encoding. Look at XML, and notice that encoding has spaces in the format. Guess that the format needs to have the spaces removed.
Graham is spot on, further from the xml spec 4.3.3:
In an encoding declaration, the values " UTF-8 ", " UTF-16 ", " ISO-10646-UCS-2 ", and " ISO-10646-UCS-4 " should be used for the various encodings and transformations of Unicode / ISO/IEC 10646, the values " ISO-8859-1 ", " ISO-8859-2 ", ... " ISO-8859- n " (where n is the part number) should be used for the parts of ISO 8859, and the values " ISO-2022-JP ", " Shift_JIS ", and " EUC-JP " should be used for the various encoded forms of JIS X-0208-1997. It is recommended that character encodings registered (as charsets) with the Internet Assigned Numbers Authority [IANA-CHARSETS], other than those just listed, be referred to using their registered names; other encodings should use names starting with an "x-" prefix. X$$anonymous$$L processors should match character encoding names in a case-insensitive way and should either interpret an IANA-registered name as the encoding registered at IANA for that name or treat it as unknown (processors are, of course, not required to support all IANA-registered encodings).
further more, from IANA spec, you will find in the table exactly what Graham was referring to, it's case insenstive but the extra spaces change everything.
IANA Encoding/Character set lookup: Characters Sets
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How do I make this XML file parser work in Unity 1 Answer
XML parsing c# in unity 1 Answer
JSON vs XML for Unity C# 1 Answer
Distribute terrain in zones 3 Answers