Category: ASP.NET

ASP.NET: Generating serialization class for XML without Xsd2Code

While there’s a free tool on Codeplex called Xsd2Code that gives you nice integration to Visual Studio’s right click menu to generate serialization class based on an XSD file, Visual Studio itself also includes a similar command line tool – the XML Schema Definition Tool (xsd.exe).

Once you have your XSD file generated from your XML file, open the Visual Studio Command Prompt and type:

xsd <YourFileName>.xsd  /c /l:cs

This will generate a C# class in the same folder with the same name as your XSD file.

Type xsd /? for more information about this tool or check the MSDN page.

Advertisements

ASP.NET: Returning CSS to client

I wasn’t a heavy back end guy but I’ve got my hands on some server control now, which returns CSS to another aspx page that expects the CSS in a <link/> tag in the page head. Something like this:

<link rel="stylesheet" type="text/css" href="Style.aspx?id=15" />

And code file of the Style.aspx page is as simple as:

string style = string.Empty;
// ...
// Grab style from DB.
// ...
Response.Clear();
Response.Write(style);
Response.End();

I realised that none of the browsers but IE recognises the styles correctly.

After a bit of trial and error + research, I found out that it was because I didn’t set the web response header correctly before return it.

What I was missing is just one line of code before calling the end for the response:

Response.AddHeader("Content-Type", "text/css");

I also found that you can’t use Response.Headers.Add("", "") to add the header entry, but didn’t go deeper looking into the reason though 🙂