Category Archives: c#

C# XML encoding woes

I have a simple XML document fragment with a euro symbol in it:
[code lang=”xml”]

Lorem ipsˆum dolor sit amet,

[/code]

which I want to load into an XML document.

[code lang=”cpp”]
System.Xml.XmlDocument doc=new System.Xml.XmlDocument();
doc.LoadXml(xml);
[/code]

I then output the XML of that doc to see that all is well – alas it is not, It has decided to re-enocde my beautiful numeric entities.
[code lang=”xml”]

Lorem ips€um dolor sit amet,

[/code]

I cannot find a way around this. It still works well, but god damnit . I want my bloody entity references back you bastards. Can anyone please help me. I have tried XMLTextReader and XML streams and all sorts of other google groups induced madness.

Update:

I managed to figure out a way to re-encode the data after it has been loaded into xml (after it has been xslt transformed or just as a string)-
[code lang=”cpp”]
System.Xml.XmlDocument docOut=new System.Xml.XmlDocument();
docOut.LoadXml(xml);
string text = docOut.OuterXml.ToString();

System.Text.StringBuilder sb = new System.Text.StringBuilder(text.Length);
foreach (char c in text)
{
if (c > 0x0080)
{
sb.Append(“&#”);
sb.Append((int)c);
sb.Append(‘;’);
}
else
{
sb.Append(c);
}
}
Log.logError(” sb.ToString(); = “+sb.ToString()+”\n”);

System.IO.StreamWriter sw = System.IO.File.CreateText(outXMLFile);
sw.Write(sb.ToString());
sw.Flush();
sw.Close();

//docOut.Save(outXMLFile);
[/code]

I’m not too sure but there might be a way to override the OuterXML property of the XMLDocument class. I hope so. For now it works and I am chuffed -now to get back to what weekends are really for, laundry and cleaning drinking 🙂

Dev crazy

In an attempt to vary the type of programming I am doing, out of office I have been working on quite a few cool projects. Outlined a few below – unfortunately none are for download yet as they are still in progress.

KarovaDev Firefox extension
As Karova‘s main product is a hosted e-commerce solution, we naturally have to integrate with quite a few payment service providers. It can be a chore to test our stores with these payment service providers, having to enter dummy or real credit card details everytime. I hate monkey work so I wrote a firefox extension for the Karova developers to help in the testing of Karova stores. It’s pretty sweet, You can edit all the default data passed for each payment service providers, specify the domains or virtual directories (if installed on localhost) to test against. The final test page scans all the user’s cookies and chooses the ones that match the supplied domains etc. You can then choose your site and test to your heart’s content- saving hours of monkey work.
Screenshot of KarovaDev firefox extension
It was nice to work on a beefy extension again. It re-affirmed to me how good the Gecko/ XUL platform is.
Philroche Blog Tool
Most of the C# work I do in wrk is web applications running on IIS. I do some command line apps that run as scheduled tasks but I don’t have cause to develop any GUI apps. Microsoft released Visual Studio 2005 express so I downloaded it and gave WinForms a shot… easy peasy to get a nice GUI designed and built. I decided to write a simple RSS aggregator to see how easy it would be.
Philroche Blog tool view feed
I haven’t written the RSS parser yet but I have the tree view and the Mozilla ActiveX control working well. I could have used the built in webbrowser (IE) component but then why would I want to do that.
Philroche Blog tool make post
I wanted to see if I could integrate with the wordpress API too to make blog posts so I turned to Xstandard (the lite version). Xstandard also have a .NET component that you can use. It is really neat and you can bet your house on the markup being valid.

Visual studio 2005 express is excellent and free too (as in beer). Also checkout SQL server 2005 express which I have used on a very beefy project already and it is ace – also free (as in beer).

Mod_Python
Since having a fairly new Dell machine donated to me by my Dad I have been feverishly using Ubuntu. I have Apache and mod_python running and have a nice little app running. Mod_python as a development platform is sweet as it is not as contstrained as TurboGears or Ruby on Rails. It is very hard to debug though.

So all in all I am getting some nice variety in my dev work, I find it can sometimes get stale if you’re doing the same thing day-in day-out, especially if it involves legacy asp sites. Is there anything you guys would recommend trying out as an antidote to work work while still being dev work (if you know what I mean). I do want to look at mono and PyGTK.

ASPX craziness

There I was hammering away, well in the zone writing a pretty beefy login system in C# when all of a sudden all the pages stopped working. I was running the application on my own machine so I tried to debug by setting a breakpoint and attaching the aspnet worker process. Nope just output blank pages. I was about to go back to a previous version of the project I had in CVS but I had done a good bit of work and didn’t fancy trying to merge it all back in bit by bit. I then noticed the following on another aspx.cs file that I had not been working on .

[code lang=”cpp”]
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

///

/// Required method for Designer support – do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
[/code]

I had seen this in my login.aspx.cs but stripped it out as thought it was the usual Visual studio bullshit. I added it and hey presto, I have XHTML being output and I can debug to my hearts content.

The moral of todays little tale – Don’t ever delete something if you don’t know what it does.

That’s enough for today, I’m going home (in my Volvo 🙂 )