first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

LINQ to XML: Two Ways of Generating the xml:space Attribute

This is our “spacey” string:

String s = @"

This is a spacey string... ";

The first method is to “brutally” use the XElement.Parse() method:

XElement spacey = XElement.Parse(
    String.Format("<spacey xml:space=\\"preserve\\">{0}</spacey>", s) );

The second (preferred) method builds with .NET 3.5 types in System.Xml.Linq:

XName xmlAttributeName = XNamespace.Xml + "space";
XAttribute xmlAttribute = new XAttribute( xmlAttributeName, "preserve" );

XElement spacey =
    new XElement( "spacey", new Object[] { xmlAttribute, s } );

XElement document = new XElement( "root", spacey );

Console.WriteLine( document.ToString() );
mod date: 2008-03-23T04:00:17.000Z