Encoding Help Needed.

July 19th, 2006 § 4 comments § permalink

Here’s the plan: There’s an XML file that contains plain text data.

[Attempt 1]: Use PHP and some simple parsing to convert the data to look pretty.
[Results]: Crazy characters like “Joan Miró” and “peoples’”

[Attempt 2]: Use C# to parse the XML and output the file.
[Results]: Crazy characters like “Joan Miró” and “peoples’”

After a bunch of research, I found that the TextWriter class can encode the file:
TextWriter tw = new StreamWriter(“fileName” + “.txt”);
TextWriter tw = new StreamWriter(“fileName” + “.txt”, false, Encoding.Default);
TextWriter tw = new StreamWriter(“fileName” + “.txt”, false, Encoding.ASCII);
TextWriter tw = new StreamWriter(“fileName” + “.txt”, false, Encoding.UTF8);

All of them didn’t work. I’ve been using Ultra Edit for a while and it can do multiple file conversions. So I give it a try…ASCII to Unicode, UTF-8 to Unicode, UTF-8 to ASCII, Unicode to ASCII, DOS to UNIX, UNIX/MAC to DOS.

It all comes down to set the C# encoding to Encoding.Default and then converting the file from UTF-8 to ASCII. There’s no other way. It sucks. Any suggestions?

UPDATE
This was what I originally had at the top of the XML file.
<?xml version=1.0 encoding=utf-8?>

This was what I now have at the top of the XML file.
<?xml version=1.0 encoding=utf-16?>

I also had to change the TextWriter initialization from:
TextWriter tw = new StreamWriter(“fileName” + “.txt”);

to:
TextWriter tw = new StreamWriter(“fileName” + “.txt”, false, Encoding.Default);

Thanks, Abhi and kashif for the input.

BTW: “PHP assumes your XML is in ISO-8859-1!” Even if you have it set as UTF-8. This is also why PHP isn’t going to work for these files, BOO. If there is a real PHP solution, let me know. We’re also trying another approach using a third party PHP DB interface.

Language/IDE Comparisons, Part 1.

March 12th, 2006 § 0 comments § permalink

Every once in a while, I’ll post up some new headaches, awesome-ness, or just differences that have come up for me using Java(1.4.2)/Eclipse (3.1.2) and C#(1.0)/Visual Studio 2003. Here are some to start:

IDE Features:
-Eclipse’s refactoring and perspectives are really helping me fly through the code and find what I need to find.
-Visual Studio 2003’s (VS2003) CTRL+Tab is much easier than Eclipse’s CTRL+F6 to move around open files.
-Eclipse’s support for JUnit testing, CTRL+X or CTRL+D, then T.
-VS2003’s support: a separate application, blech.

Parenthesis and curly braces:
-Eclipse auto closes it for you, VS doesn’t.
-VS2003 auto indents after you close it, Eclipse doesn’t.

Text navigation; CTRL (+ C) left and right
-Eclipse selects by word-“humps” i.e. thisClass has 2 humps, oneTwoThree has 3, etc.
-VS selects by word alone.

Langauage Features:
Verbatim string literals:
-They are a billion times prettier regular string literals. C# has them, Java doesn’t.

(C#)
CSharpSqlStatement = @“SELECT *
FROM table1 t1
WHERE t1.column1 = ‘seacrhText’
ORDER BY t1.column1″;

(Java)
javaSqlStatement = “SELECT * “
+ ” FROM table1 t1 “
+ ” WHERE t1.column1 = ‘seacrhText’ “
+ ” ORDER BY t1.column1″;

Try copy and pasting a statement from the SQL editor over to your code, then formatting it so Java can understand it. With C#, Copy and Paste inside parenthesis and let it know its a string literal with a ‘@‘.

Learning XPath.

November 5th, 2005 § 0 comments § permalink

So I need to parse some XML for my SDP project (C# windows application). I know you can parse XML with the regular .NET Xml class, but I saw XPath. It’s bad ass. It navigates through your XML file with ease. Check out the sample:

//Load the XML document
XmlTextReader(“trafficInfo.xml”);
XmlDocument doc = new XmlDocument();
doc.Load(new XmlTextReader(“trafficInfo.xml”));

//Create XPathNavigator
XPathNavigator xpathNav = doc.CreateNavigator();

//Create XPathIterator
string xpathStr = “/items/current/city”;
XPathNodeIterator xIterator = xpathNav.Select(xpathStr);

MessageBox.Show(xIterator.Current.Value.ToString());

//XML file
<?xml version=”1.0″ encoding=”UTF-8″ ?>
<items>
   <current>
      <city>Detroit</city>
   </current>
</items>

More Articles on XPath:
http://perfectxml.com Article
MSDN Library Article

P.S. I wish Blogger had tags. It’d make things so much more organized.

C# and C++.

October 4th, 2005 § 1 comment § permalink

So I’ve been doing C# for a while. This ACM competition we’re practicing for is AWESOME, but it’s in C++!

C# babys programmers and abstracts the really hardcore stuff away from the users, which is good, but it has version 2.0 coming out and the version 3.0 features are already being published.

C++ doesn’t have a version 2 coming out, so we can all go to http://www.cplusplus.com which won’t be changing in the near future, or even at all, and get all the references we need.

Which to pick? BOTH. You gotta know the both, not one or the other…BOTH: TWO, DOS, DEAUX, HAI, 2, 10!!! Time to open up those old school C++ books.

Where Am I?

You are currently browsing entries tagged with csharp at thomas nguyen.