Simon Fell > Its just code > XmlWriter

Sunday, April 6, 2003

Giulio Piancastelli is using XOM to generate RSS fragments. I use XmlWriter which is one of things I miss most when I'm not coding in .NET
private string makePayload()
    const string TUNE_NS = "" ;
    const string DC_NS   = "" ;
    StringWriter sw = new StringWriter() ;
    XmlTextWriter xw = new XmlTextWriter(sw) ;
    xw.WriteStartElement("item") ;
    xw.WriteElementString("title", title.Text.Trim()) ;
    xw.WriteElementString("description", data.Text.Trim()) ;
    xw.WriteElementString("link", link.Text.Trim()) ;
    DateTime dt = DateTime.Now ;
    if ( date.Text.Length > 0 )
        dt = DateTime.Parse(date.Text) ;
    xw.WriteElementString("date", DC_NS, dt.ToUniversalTime().ToString("s") + "Z" ) ;
    xw.WriteElementString("tune", TUNE_NS, tune.Text.Trim()) ;
    xw.WriteEndElement() ;
    xw.Close() ; 
    return sw.ToString() ;
In fact the whole of System.Xml rocks, with one exception, using SAX its trivial to chain a bunch of SAX handlers together to do interesting/extensible things, whilst this is possible with XmlReader and/or XmlWriter, the number of methods in these classes make it cumbersome. To me they need splitting up into a small core interface, and a helper object that provides all the extra functions (most of the functions in XmlWriter are helpers than could be built on top of the core interface).