OmitXmlDeclaration with xDocument

  • Thread starter Thread starter Markus Freitag
  • Start date Start date
M

Markus Freitag

Guest
Hello!
Is there no easier way to save an XML without the header?
We already have the save method in the doc.

<?xml version="1.0" encoding="utf-8"?>


//My Code:

XDeclaration dec = new XDeclaration(null, null, null);
XElement localRoot = new XElement("MY_ROOT");


XDocument doc = new XDocument(dec, localRoot);


localRoot.Add(new XElement("FIELD",
new XElement("NAME", new XAttribute("value", "PanelCode")),
new XElement("VALUE", new XAttribute("value", "--")),
new XElement("GROUP", new XAttribute("value", "3"))));
doc.Save(Path.Combine(dBPathAbsolute, $"Instances\\JOB_{taskname}.XML"));


XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;
StringWriter sw = new StringWriter();
using (XmlWriter xw = XmlWriter.Create(sw, settings))
{
doc.Save(xw);
}
string xmlContent = sw.ToString();

using (StreamWriter writetext = new StreamWriter(Path.Combine(dBPathAbsolute, $"Instances\\TASK_{taskname}.XML"), false))
{
writetext.WriteLine(xmlContent);
}

With best regards Markus

Continue reading...
 
Back
Top