2009-04-20 21 views
14

Estoy editando archivos csproj con Linq-to-XML y necesito guardar el XML sin el encabezado <?XML?>.XDocument.Save() sin encabezado

Como a XDocument.Save() le falta la opción necesaria, ¿cuál es la mejor manera de hacerlo?

Respuesta

22

Usted puede hacer esto con XmlWriterSettings, y guardar el documento a un XmlWriter:

XDocument doc = new XDocument(new XElement("foo", 
    new XAttribute("hello","world"))); 

XmlWriterSettings settings = new XmlWriterSettings(); 
settings.OmitXmlDeclaration = true; 
StringWriter sw = new StringWriter(); 
using (XmlWriter xw = XmlWriter.Create(sw, settings)) 
// or to write to a file... 
//using (XmlWriter xw = XmlWriter.Create(filePath, settings)) 
{ 
    doc.Save(xw); 
} 
string s = sw.ToString(); 
+0

realmente debería haber buscado en Google esto antes. Recuerdo que busqué hacer algo similar y poner algunos hilos terribles para reemplazar el truco para que funcione. Buen hallazgo :) –

+0

@Johannes - yup! Uy ... arreglado, gracias. –

+0

Funciona perfectamente, gracias! – laktak

Cuestiones relacionadas