Csharp

Handling Multiple QueryString Parameters With the Same Key in ASP.NET

Handling Multiple QueryString Parameters With the Same Key in ASP.NET
When you are processing an HTTP request in ASP.NET you can retrieve the user-provided query string parameters using the HttpRequest.QueryString property. This property is an instance of the NameValueCollection class. If the user has provided multiple parameters with the same key in the query string, HttpRequest.QueryString[key] will return all the values concatenated together with commas. If you would rather process the values individually, use HttpRequest.QueryString.GetValues(key), which will return an array of all the provided values.

XmlTextWriter Can Produce Invalid XML

XmlTextWriter Can Produce Invalid XML
XmlTextWriter is .NET’s class for writing XML in a forward-only streaming manner. It is highly efficient and is the preferred way to generate XML in .NET in most circumstances. I find XmlTextWriter so useful I wrote a partial C++ implementation of it in Implenting IXmlWriter Series. Unfortunately, XmlTextWriter isn’t quite as strict as it could be. It will let slip some invalid XML such as duplicate attributes, invalid Unicode characters in the range 0×0 to 0×20, and invalid element and attribute names.

How Return XML From ASPX in ASP.NET 1.1

How Return XML From ASPX in ASP.NET 1.1
I’m not sure if this is the “canonical” way to do it but here’s a description of how to write an ASP.NET 1.1 ASPX page which returns a XML document (e.g. when writing a home-brewed web service). First, create a new Web Form (I will call it WebService.aspx). As we will be progamatically generating the XML in the HTTP response rather than sending the (processed) content of the ASPX file, delete everything from the ASPX file but the @Page directive, so that it looks something like:

Implementing IXmlWriter

Implementing IXmlWriter
This is my implementing IXmlWriter post series The Basics Escaping Element Content Supporting WriteElementString() Collapsing Empty Elements Supporting WriteAttributeString() Escaping Attribute Content Cleaning Up Supporting WriteStartAttribute() and WriteEndAttribute() Supporting WriteStartDocument() and WriteEndDocument() Supporting WriteComment() Supporting Namespaces Supporting Pretty-Printing Putting IXmlWriter Behind A Pimpl Firewall Supporting Writing To A Stream

Revisiting Excel Interop

Revisiting Excel Interop
I ran into problem today relating to Excel interop. A coworker made a change to a C# application I wrote and was trying to build it. The program relied on a project which had a reference to the Microsoft Excel 9.0 Object Library which ships with Office 2000. However, the coworker had Office 2003 installed which includes the Excel 11.0 Object Library and not the Excel 9.0 Object Library. Because of this, he could not build the application.

Deterministic Finalization and IDisposable Part 5: Useful IDisposable Class 3: AutoReleaseComObject

Deterministic Finalization and IDisposable Part 5: Useful IDisposable Class 3: AutoReleaseComObject
This is part 5 of my Deterministic Finalization and IDisposable post series. This is the final example in my series on deterministic finalization in garbage-collected languages and the true motive behind the series: AutoReleaseComObject. The idea behind AutoReleaseComObject is simple: it is nothing but a wrapper around a COM object which calls Marshal.ReleaseComObject() upon Dispose() until the COM object’s reference count is 0 and the object is freed. Here’s the implementation:

IDisposable

IDisposable
This is my IDisposable post series The Basics Rules For Implementing IDisposable Useful IDisposable Class 1: TimedLock Useful IDisposable Class 2: AutoDeleteFile Useful IDisposable Class 3: AutoReleaseComObject

Deterministic Finalization and IDisposable Part 4: Useful IDisposable Class 2: AutoDeleteFile

Deterministic Finalization and IDisposable Part 4: Useful IDisposable Class 2: AutoDeleteFile
This is part 4 of my Deterministic Finalization and IDisposable post series. I guess my definition of tomorrow is much longer than I thought, but here’s another useful IDisposable class which I shall present without comment: AutoDeleteFile. using System; using System.Diagnostics; using System.IO; /// <summary> /// A file wrapper which automatically deletes the file unless Disarm() /// is called. /// </summary> public sealed class AutoDeleteFile : IDisposable { private FileInfo m_underlyingFile; private bool m_armed = true; private bool m_disposed = false; public AutoDeleteFile(FileInfo underlyingFile) { Debug.

Deterministic Finalization and IDisposable Part 3: Useful IDisposable Class 1: TimedLock

Deterministic Finalization and IDisposable Part 3: Useful IDisposable Class 1: TimedLock
This is part 3 of my Deterministic Finalization and IDisposable post series. For the first example of a useful custom class which implements IDisposable, I will simply link to and reproduce Ian Griffith’s TimedLock — an enhancement of the C# lock statement which allows the specification of a timeout period instead of blocking forever while trying to obtain the lock. The code for TimedLock is reproduced below: using System; using System.