eWorld.UI - Matt Hawley

Ramblings of Matt

Reading those Pesky Visual Studio Project Files

October 7, 2004 19:40 by matthaw

Way back when I implemented VS.NET project files as a source for Unleash It, a few users submitted bugs that stated they were receiving error messages when loading their VS.NET project file in Unleash It. I looked into it, and noted that their problems related to non-encoded properly characters in the project file.

Sure, the project files look like XML - but don't get fooled, most of the time they will be valid, but those very few pesky project files will blow the top off of Mount St. Helens (again). It seems that Microsoft decided to roll their own XML parsing engine for reading / writing these project files.

So, onward I went with a temporary fix that allowed me to bypass the few exceptions...then today, another user stated they were having the same problem. What! No! Huh! Yeah, the problem still existed, but this time it wouldn't complain, just throw a nasty error message that Unleash It couldn't handle.

So I turned a new leaf, tried many new different things, then resorted talking to my buddy Jeff Key (who now I owe so many beers that he'll be drinking for free on several occasions). We both cracked down and figured it was something to do with the encoding...hmm, what if we encoded it to UTF-8? Ahh, yes, that works - but now, how can we do this on the fly without modifying the Users' project file? Store the UTF-8 version in memory of course. And so, 5 minutes later, Jeff had a working example of how to do this. I quickly plugged it into my code base...and WAHLA! It works!

So, below is the code to read in a Project file using XmlTextReader that will (hopefully) not cause any problems...

StreamReader sr = File.Open(projectFileLocation);
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms, System.Text.Encoding.UTF8);
sw.Write(sr.ReadToEnd());
sw.Flush();

ms.Position = 0;
XmlTextReader reader = new XmlTextReader(ms);

while (reader.Read())
{
    ....
}

reader.Close();
ms.Close();
sw.Close();

Enjoy! Just make sure you make the changes to your code as well, because you're likely to come across this problem at some point or another as well.

Categories: .NET
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed
Comments are closed

Copyright © 2000 - 2024 , Excentrics World