How to close a XML Element using XmlTextWriter

Recently with my forays into the XMPP land, I have needed to handle the case of writing out the following XML (as is, without the closing tag):

Single opening tag
<stream:stream xmlns="http://etherx.jabber.org/streams"  xmlns="jabber:client" from="bob" to="server">

Note that the tag is closed. The .Net XML writer keeps that open until you try and go into a conflicting state such as a new start tag, comment, PI, or such. Calling Flush() simply doesn’t work.

My previous methodology used a comment to close it. Thus something like this was sent out:

Using a comment to transfer state
<stream:stream xmlns="http://etherx.jabber.org/streams"  xmlns="jabber:client" from="bob" to="server"><!-- Start Stream -->

Not the best. Some clients dont like comments and the XMPP specification says that comments SHOULD NOT be used (note, not MUST NOT), so some parsers fall over.

I finally decided to give fixing it a go. I created a root XML writer class that had one abstract method: CompleteElement(). I won’t paste that class in because it is trivial. I cracked open Reflector and figured out if there was a common method in XmlTextWriter that handles this. I was in luck, there was (AutoComplete)! My first attempt simply reflected over the XmlTextWriter and found the method, enum and enum field. It didn’t work. XmlWriter.Create() hands out XmlWellFormedWriters (you can’t instantiate these directly, the class is internal). So I looked at it using Reflector and I was in luck again! The only thing that is different is the name of the method (AdvanceState) and everything else was exactly the same.

Here it is (most of it is purely wrappers):

Complete Streaming XML Writer
    /// <summary>
    /// Represents a streaming xml text writer.
    /// </summary>
    public class StreamingXmlTextWriter : StreamingXmlWriter
    {
        private static object __autoCompleteComment;
        private static MethodInfo __autoCompleteMethod;

        static StreamingXmlTextWriter()
        {
            // Get the type.
            Type wellFormedWriter = typeof(XmlTextWriter).Assembly.GetType("System.Xml.XmlWellFormedWriter");

            // Find the method.
            __autoCompleteMethod = wellFormedWriter.GetMethod("AdvanceState", BindingFlags.Instance | BindingFlags.NonPublic);

            // Find the argument.
            Type tokenEnum = wellFormedWriter.GetNestedType("Token", BindingFlags.NonPublic);
            FieldInfo tokenField = tokenEnum.GetField("Comment", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            __autoCompleteComment = tokenField.GetValue(null);
        }

        private XmlWriter writer;
        public StreamingXmlTextWriter(Stream stream, Encoding encoding)
            : base(stream, encoding)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = encoding;
            settings.OmitXmlDeclaration = true;

            writer = XmlWriter.Create(stream, settings);
        }

        #region Wrapped Methods
        public override void Close()
        {
            writer.Close();
        }

        public override void Flush()
        {
            writer.Flush();
        }

        public override string LookupPrefix(string ns)
        {
            return writer.LookupPrefix(ns);
        }

        public override void WriteBase64(byte[] buffer, int index, int count)
        {
            if (skippedAttribute)
                return;

            writer.WriteBase64(buffer, index, count);
        }

        public override void WriteCData(string text)
        {
            if (skippedAttribute)
                return;

            writer.WriteCData(text);
        }

        public override void WriteCharEntity(char ch)
        {
            if (skippedAttribute)
                return;

            writer.WriteCharEntity(ch);
        }

        public override void WriteChars(char[] buffer, int index, int count)
        {
            if (skippedAttribute)
                return;

            writer.WriteChars(buffer, index, count);
        }

        public override void WriteComment(string text)
        {
            writer.WriteComment(text);
        }

        public override void WriteDocType(string name, string pubid, string sysid, string subset)
        {
            writer.WriteDocType(name, pubid, sysid, subset);
        }

        public override void WriteEndAttribute()
        {
            if (skippedAttribute)
            {
                skippedAttribute = false;
                return;
            }

            writer.WriteEndAttribute();
        }

        public override void WriteEndDocument()
        {
            writer.WriteEndDocument();
        }

        public override void WriteEndElement()
        {
            writer.WriteEndElement();
        }

        public override void WriteEntityRef(string name)
        {
            if (skippedAttribute)
                return;

            writer.WriteEntityRef(name);
        }

        public override void WriteFullEndElement()
        {
            writer.WriteFullEndElement();
        }

        public override void WriteProcessingInstruction(string name, string text)
        {
            writer.WriteProcessingInstruction(name, text);
        }

        public override void WriteRaw(string data)
        {
            writer.WriteRaw(data);
        }

        public override void WriteRaw(char[] buffer, int index, int count)
        {
            writer.WriteRaw(buffer, index, count);
        }

        private bool skippedAttribute;
        public override void WriteStartAttribute(string prefix, string localName, string ns)
        {
            // XSI/XSD must not be emitted.
            if (prefix == "xmlns" || localName == "xmlns")
            {
                if (localName == "xsi" || localName == "xsd")
                {
                    skippedAttribute = true;
                    return;
                }

                ApplyNamespace(prefix, localName, ref ns);
            }
            writer.WriteStartAttribute(prefix, localName, ns);
        }

        public override void WriteStartDocument(bool standalone)
        {
            writer.WriteStartDocument(standalone);
        }

        public override void WriteStartDocument()
        {
            writer.WriteStartDocument();
        }

        public override void WriteStartElement(string prefix, string localName, string ns)
        {
            writer.WriteStartElement(prefix, localName, ns);
        }

        public override System.Xml.WriteState WriteState
        {
            get { return writer.WriteState; }
        }

        public override void WriteString(string text)
        {
            if (skippedAttribute)
                return;

            writer.WriteString(text);
        }

        public override void WriteSurrogateCharEntity(char lowChar, char highChar)
        {
            if (skippedAttribute)
                return;

            writer.WriteSurrogateCharEntity(lowChar, highChar);
        }

        public override void WriteWhitespace(string ws)
        {
            if (skippedAttribute)
                return;

            writer.WriteWhitespace(ws);
        }

        public override XmlWriterSettings Settings
        {
            get
            {
                return writer.Settings;
            }
        }

        public override string XmlLang
        {
            get
            {
                return writer.XmlLang;
            }
        }

        public override XmlSpace XmlSpace
        {
            get
            {
                return writer.XmlSpace;
            }
        }
        #endregion

        public override void CompleteElement()
        {
            PerformAutoComplete();
        }

        private void PerformAutoComplete()
        {
            __autoCompleteMethod.Invoke(writer, new object[] { __autoCompleteComment });
        }
    }
Posted in C#, Migrated, Programming, Top Hacks | Leave a comment

TFS Build Server 2005 with VS 2008 Solutions

I am currently RNDing Continuous Integration for our build process, as well as automating certain build processes that we use throughout the company.

One issue that I found is that TFS Build Server 2005 plain refuses to build Visual Studio 2008 solution files, and after some research I found that it is because of the SLN version number. One method involves using a shim in place of MSBuild to call the .Net 3.5 MSBuild executable. The current shims are really weak and don’t do the job properly, so I have made one that does.

It handles incoming input, as well as the standard out and error streams. This will allow MSBuild to report back to the server on things such as compile errors.

To use the tool, rename shim.exe to the name of the original file (in this case MSBuild.exe) and replace that file. Open regedit and create (or open) a key at HKLM\Software\jonathand\Shim. In this key place a new string value with the original file as the name and the new file as the value. The shim will now call the file as it should. This should theoretically also help with MSTest.exe (I think that’s the name) when it comes to integrated unit tests.

Shim tool
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using Microsoft.Win32;
using System.Diagnostics;
using System.Threading;

namespace Shim
{
    class Program
    {
        private static Process _process;

        static int Main(string[] args)
        {
            if (TargetFile == "")
            {
                Console.Error.WriteLine("No target for shim found in registry for '{0}'.", EntryAssemblyFileName);
                return 9009; // File not found.
            }

            // Create process.
            _process = new Process();
            _process.StartInfo.Arguments = Arguments;
            _process.StartInfo.CreateNoWindow = true;
            _process.StartInfo.FileName = TargetFile;
            _process.StartInfo.RedirectStandardError = true;
            _process.StartInfo.RedirectStandardInput = true;
            _process.StartInfo.RedirectStandardOutput = true;
            _process.StartInfo.UseShellExecute = false;
            _process.StartInfo.WorkingDirectory = Environment.CurrentDirectory;

            // Wire up events.
            _process.OutputDataReceived += new DataReceivedEventHandler(_process_OutputDataReceived);
            _process.ErrorDataReceived += new DataReceivedEventHandler(_process_ErrorDataReceived);
            Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);

            _process.Start();
            _process.BeginErrorReadLine();
            _process.BeginOutputReadLine();

            // Create read thread.
            Thread readThread = new Thread(ReaderWorker);
            readThread.IsBackground = true; // Terminate when I terminate.
            readThread.Start();

            _process.WaitForExit();

            return _process.ExitCode;
        }

        static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
        {
            e.Cancel = true;
            _process.Kill();
        }

        static void ReaderWorker()
        {
            while (true)
            {
                string data = Console.ReadLine();
                _process.StandardInput.WriteLine(data);
            }
        }

        static void _process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
            Console.Error.WriteLine(e.Data);
        }

        static void _process_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            Console.Out.WriteLine(e.Data);
        }

        private static string _targetFile;
        private static string TargetFile
        {
            get
            {
                if (_targetFile == null)
                {
                    using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\jonathand\\Shim"))
                    {
                        if (key == null)
                            _targetFile = "";
                        else
                            _targetFile = (string)key.GetValue(EntryAssembly.ToLowerInvariant(), "");
                    }
                }
                return _targetFile;
            }
        }

        private static string _arguments;
        private static string Arguments
        {
            get
            {
                if (_arguments == null)
                {
                    _arguments = Environment.CommandLine;
                    int remString = EntryAssembly.Length + 1;
                    if (_arguments.StartsWith("\""))
                        remString += 2;
                    _arguments = _arguments.Remove(0, remString);
                }
                return _arguments;
            }
        }

        private static string _entryAssembly;
        static string EntryAssembly
        {
            get
            {
                if (_entryAssembly == null)
                {
                    _entryAssembly = Assembly.GetExecutingAssembly().CodeBase;
                    if (_entryAssembly.StartsWith("file:///"))
                        _entryAssembly = _entryAssembly.Remove(0, 8);
                    _entryAssembly.Replace('/', Path.DirectorySeparatorChar);
                }
                return _entryAssembly;
            }
        }

        private static string _entryAssemblyFileName;
        private static string EntryAssemblyFileName
        {
            get
            {
                if (_entryAssemblyFileName == null)
                    _entryAssemblyFileName = Path.GetFileName(EntryAssembly).ToLowerInvariant();
                return _entryAssemblyFileName;
            }
        }
    }
}
Posted in C#, Migrated, Programming, Top Hacks | Leave a comment

Anonymous Configuration Elements

Introduction

Recently, with my XMPP server that is RND I wanted to make an extensible transport system. This would allow the user to build their own transport types (such as BOSH, or hell, even Named Pipes) and wire them into the server. Each type of transport obviously has different configuration options (such as Address and Port for TCP and Name for Named Pipes). This posed an interesting challenge, how do I allow the users to add whatever attributes they want to the configuration?

Configuration data
<connectTo>
  <add target="Transports.TcpTransport, Transports, ..." address="192.168.0.5" port="5222" />
  <add target="Transports.NamedPipeTransport, ..." name="\\.\xmppServer"/>
</connectTo>

So how can we create the functionality without hacking stuff to death?

How I did it

My first clue was the overridable method, OnDeserializeUnrecognizedAttribute(). After overriding it and setting a breakpoint I found it worked perfectly. Now the only trick was to differentiate between declared properties and properties that we have explicitly set.

Luckily the base class didn’t have a wierd implementation, so calling the base.Properties gave me the functionality I needed, so I simply stored those properties in another collection so that I knew which ones where defined dynamically by the user.

The Code

Well, here is the code to do all you have ever needed :), well not really. In any case, it has turned out to be very useful to me.

Anonymous Configuration Elements
    /// <summary>
    /// Represents a configuration element that can support properties not
    /// defined at compile time.
    /// </summary>
    public class AnonymousValueConfigurationElement : ConfigurationElement
    {
        private List<string> _supportedProperties = new List<string>();

        /// <summary>
        /// Gets the list of properties that are unknown.
        /// </summary>
        public IEnumerable<KeyValuePair<string, string>> UnknownProperties
        {
            get
            {
                foreach (ConfigurationProperty prop in Properties)
                {
                    // Only return properties that we don't explicitly support.
                    if (!_supportedProperties.Contains(prop.Name))
                    {
                        yield return new KeyValuePair<string, string>(prop.Name, (string)this[prop.Name]);
                    }
                }
            }
        }

        private ConfigurationPropertyCollection _properties;
        /// <summary>
        /// Gets the combination of all the properties (known and unknown).
        /// </summary>
        protected override ConfigurationPropertyCollection Properties
        {
            get
            {
                // Have we done this yet?
                if (_properties == null)
                {
                    // Make our property collection.
                    _properties = new ConfigurationPropertyCollection();

                    ConfigurationPropertyCollection col = base.Properties;
                    foreach (ConfigurationProperty prop in col)
                    {
                        // Add it.
                        _properties.Add(prop);
                        // Add it to our information about the supported properties.
                        _supportedProperties.Add(prop.Name);
                    }
                }
                // Return them.
                return _properties;
            }
        }

        protected override bool OnDeserializeUnrecognizedAttribute(string name, string value)
        {
            // Make a new property.
            ConfigurationProperty prop = new ConfigurationProperty(name, typeof(string));
            // Add a new property so that we can set it.
            Properties.Add(prop);
            // Set it.
            this[name] = value;
            // We handled it.
            return true;
        }
    }

It is really simple, and something that maybe should have been included in the framework, but for those who are wondering how to do it; maybe your search is over.

Hope this helps!

Posted in C#, Migrated, Programming | Leave a comment