MangoMarcus
asked on
Line Breaks in XML Files
How do I get a line break or carriage retunr in the actual XML file so it does not go across one line. I have tried the XMLWriterSettings but it does not appear to work. The code is as below:
ublic partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// location to the XML file to write
String strFilePath = "c:\\Projects\\company\\Ap p_Data\\mo vies.xml";
// create an instance of the XmlTextWriter object
XmlTextWriter objWriter = new XmlTextWriter(strFilePath, System.Text.Encoding.UTF8) ;
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaratio n = true;
settings.NewLineOnAttribut es = true;
objWriter.WriteStartElemen t("order") ;
objWriter.WriteAttributeSt ring("orde rID", "367A54");
objWriter.WriteAttributeSt ring("date ", "2001-05-03");
objWriter.WriteElementStri ng("price" , "19.95");
objWriter.WriteEndElement( );
objWriter.Flush();
// clear up memory
objWriter.Close();
// successfully done
result.Text = "XML file ";
}
}
ublic partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// location to the XML file to write
String strFilePath = "c:\\Projects\\company\\Ap
// create an instance of the XmlTextWriter object
XmlTextWriter objWriter = new XmlTextWriter(strFilePath,
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaratio
settings.NewLineOnAttribut
objWriter.WriteStartElemen
objWriter.WriteAttributeSt
objWriter.WriteAttributeSt
objWriter.WriteElementStri
objWriter.WriteEndElement(
objWriter.Flush();
// clear up memory
objWriter.Close();
// successfully done
result.Text = "XML file ";
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Cheers Ashley, worked a treat!
(Thanks Anarki - its seems to throw an error when you swap (strFilePath, System.Text.Encoding.UTF8) for (strFilePath, System.settings) - obviously something im doing wrong!)
(Thanks Anarki - its seems to throw an error when you swap (strFilePath, System.Text.Encoding.UTF8)
ASKER
Sorry Anarki, I meant (strFilePath, settings)
You need to create settings before creating a writer and pass this settings object to a writer constructor,
something like
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaratio
settings.NewLineOnAttribut
XmlTextWriter writer = XmlTextWriter.Create("book