Writing XMLDocument to file with specific newline character (c#)

后端 未结 2 636
情歌与酒
情歌与酒 2021-01-18 01:00

I have an XMLDocument that I have read in from file. The file is Unicode, and has the newline character \'\\n\'. When I write the XMLDocument back out, it has the newline ch

2条回答
  •  别那么骄傲
    2021-01-18 01:48

    I think you're close. You need to create the writer from the settings object:

    (Lifted from the XmlWriterSettings MSDN page)

    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    settings.OmitXmlDeclaration = true;
    settings.NewLineOnAttributes = true;
    
    writer = XmlWriter.Create(Console.Out, settings);
    
    writer.WriteStartElement("order");
    writer.WriteAttributeString("orderID", "367A54");
    writer.WriteAttributeString("date", "2001-05-03");
    writer.WriteElementString("price", "19.95");
    writer.WriteEndElement();
    
    writer.Flush();
    

提交回复
热议问题