Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Why is my code not reading the $ symbol, please?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace XMLRead
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlTextReader reader = new XmlTextReader("book.xml");
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element: // The node is an element.
                        Console.Write("<" + reader.Name);
                        Console.WriteLine(">");
                        break;
                    case XmlNodeType.Text: //Display the text in each element.
                    //    Console.WriteLine(reader.Prefix);
                        Console.WriteLine(reader.Value);
                        break;
                    case XmlNodeType.EndElement: //Display the end of the element.
                        Console.Write("</" + reader.Name);
                        Console.WriteLine(">");
                        break;
                }
            }
        }
    }
}


book.xml
<bookstore>
<book>
<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>
<title>
The Confidence Man
</title>
<author>
<first-name>
Herman
</first-name>
<last-name>
Melville
</last-name>
</author>
<price>
$11.99
</price>
</book>
<book>
<title>
The Gorgias
</title>
<author>
<name>
Plato
</name>
</author>
<price>
$9.99 
</price>
</book>
</bookstore>
Posted
Comments
Kornfeld Eliyahu Peter 9-Jun-14 16:20pm    
I run your code and got everything as expected. The price tag printed with the dollar ($) sign, like $8.99, $11.99 and so...
You may try to debug it to see what went wrong. Also may check the encoding of your xml file...

The fragment of code you show is one big abuse of elementary technology. Instead of directly writing all those XML angular brackets, use the class System.Xml.XmlTextReader:
http://msdn.microsoft.com/en-us/library/system.xml.xmltextreader%28v=vs.110%29.aspx[^].

And you reader does read the '$' character, because it does not have any special meaning in XML; you just fail to see that. You did not show any code which reads anything except reader.Read(); hence, if you look through the results of the read using the debugger, you will see that all elements are read properly.

—SA
 
Share this answer
 
v2
Comments
jon-80 18-Jun-14 6:34am    
Well this is helpful however, I was actually reading XML not writing :)
Sergey Alexandrovich Kryukov 18-Jun-14 13:04pm    
Oh, sorry for this confusion; of course I meant XmlTextReader. I fixed this error in my answer.
Will you formally accept the answer now (green "Accept" button)?
—SA
Well, I tried this again and Visual Studio 2010 keeps coming up with errors.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900