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() );