seco

Hi

i have books.xml file like this
--------------------------------------------------------------
< xml version="1.0" >
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
<book genre=¡±autobiography¡± publicationdate=¡±1981¡± ISBN=¡±1-861003-11-0¡±>
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre=¡±novel¡± publicationdate=¡±1967¡± ISBN=¡±0-201-63361-2¡±>
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre=¡±philosophy¡± publicationdate=¡±1991¡± ISBN=¡±1-861001-57-6¡±>
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>
---------------------------------------------------------------
and i use Xmltextreader to read from it it gives me exception says

"Invalid character in the given encoding. Line 4, position 13."
when i remove this line at all gives me other exceptions
how to solve them and read from the file
and how to write right xml next time

thanks in advance.




Re: XML and the .NET Framework Invalid character in the given encoding

Nikolay Glushnev

The error message is correct. The document is not well-formed because of invalid quotation marks '\x94' used to delimit attribute values. XML requires one of two quotation characters:

quot " U+0022 (34) XML 1.0 quotation mark (neutral quotation mark)
apos ' U+0027 (39) XML 1.0 apostrophe (neutral single quotation mark)

XmlTextWriter ensures that valid quotation mark is used.






Re: XML and the .NET Framework Invalid character in the given encoding

seco

thanks for reply

but im new to xml
could you write the right version of that file.
thanks.





Re: XML and the .NET Framework Invalid character in the given encoding

Nikolay Glushnev

For this file you can replace all occurrences of the ¡± character with ". BTW, for the ¡± character there is a matching left quote ¡°, which is equally unsuitable as an XML attribute value delimiter. Used together the pair looks like this: ¡°1981¡±, which is different from "1981".

Have you created the file with
Word If yes, then probably it was not a good idea. Use specialized XML editor such as:
1. Visual Studio built-in XML editor

2. Xml Notepad 2007. That's a free download:
http://www.microsoft.com/downloads/details.aspx familyid=72d6aa49-787d-4118-ba5f-4f30fe913628&displaylang=en
3. search the Internet for other XML editors

For quick edits a simple plain text editor like Notepad that doesn't do any automatic character substitutions in order to improve typographic appearance of the text can also be OK.

With regard to encodings, UTF-8 is used most often, that's one of standard XML encodings, and, as a rule of thumb, I'd recommend the use of UTF-8 unless you have a specific reason not to do so. Encoding name can be specified in Xml declaration:
< xml version="1.0" encoding="UTF-8" >

The corrected file:

< xml version="1.0" >
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>






Re: XML and the .NET Framework Invalid character in the given encoding

seco

thanks for reply

ok now one last thing
i write the following code to create a new book to the bookstore

Code Snippet


XmlElement xe = doc.CreateElement("049568049586");
xe.SetAttribute("ISBN", "04573452");
xe.SetAttribute("Title", "vb.net");
xe.SetAttribute("Publisher", "mark");
XmlTextWriter xt = new XmlTextWriter("___books.xml",Encoding.Default);
xt.Formatting = Formatting.Indented;
doc.WriteContentTo(xt);
xt.Close();


but nothing added to the file !!!
any help

thanks.





Re: XML and the .NET Framework Invalid character in the given encoding

Derek Smyth

Hello Seco,

Your missing just one line

Code Snippet

XmlElement xe = doc.CreateElement("049568049586");
xe.SetAttribute("ISBN", "04573452");
xe.SetAttribute("Title", "vb.net");
xe.SetAttribute("Publisher", "mark");

//a line in here that adds the element to the document,

//I don't know where you intend to add the node so I'll assume to the root node

doc.DocumentElement.AppendChild(ex);


XmlTextWriter xt = new XmlTextWriter("___books.xml",Encoding.Default);
xt.Formatting = Formatting.Indented;
doc.WriteContentTo(xt);
xt.Close();

Hope that helps