Reputation: 1041
First off, I don't like doing this. This isn't my choice.
I need to write information to an xml document. The simpleContent of the xml may contain &'s. I want these to written to the file as just &
, not &
How do I do this? I'm guessing that it includes a StreamWriter and something like:
private void SaveCaption_Click(object sender, EventArgs e)
{
if (saveCaptionDialog.ShowDialog() == DialogResult.OK)
{
using (StreamWriter writer = new StreamWriter(saveCaptionDialog.FileName, null))
{
using (XmlWriter xmlWriter = XmlWriter.Create(writer))
{
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Indentation = 4;
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("tt");
...
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
}
}
//streamWriter do your thing
}
}
Upvotes: 2
Views: 509
Reputation: 12426
Please only do this if you absolutely must; this is not standards compliant XML.
Only you can prevent XML fires.
Use the WriteRaw()
method:
XmlWriter writer;
writer = XmlWriter.Create( @"C:\users\account\desktop\test.xml" );
writer.WriteStartDocument();
writer.WriteStartElement( "TestElement" );
writer.WriteRaw( "Hello & goodbye" );
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
Which generates the output:
<?xml version="1.0" encoding="utf-8"?><TestElement>Hello & goodbye</TestElement>
Upvotes: 4