How to keep carriage returns from parsing XML

I am looking on the Internet how to save carriage returns from XML data, but I could not find the answer, so I am here :)

The goal is to write the contents of the XML data to a file. So, if the node value contains some data "\ r \ n", there is a soft need to write it to a file in order to create a new line, but it is not written even with space: save.

Here is my test class:

XElement xRootNode = new XElement("DOCS"); XElement xData = null; //XNamespace ns = XNamespace.Xml; //XAttribute spacePreserve = new XAttribute(ns+"space", "preserve"); //xRootNode.Add(spacePreserve); xData = new XElement("DOC"); xData.Add(new XAttribute("FILENAME", "c:\\titi\\prout.txt")); xData.Add(new XAttribute("MODE", "APPEND")); xData.Add("Hi my name is Baptiste\r\nI'm a lazy boy"); xRootNode.Add(xData); bool result = Tools.writeToFile(xRootNode.ToString()); 

And here is my process class:

 try { XElement xRootNode = XElement.Parse(xmlInputFiles); String filename = xRootNode.Element(xNodeDoc).Attribute(xAttributeFilename).Value.ToString(); Boolean mode = false; try { mode = xRootNode.Element(xNodeDoc).Attribute(xWriteMode).Value.ToString().ToUpper().Equals(xWriteModeAppend); } catch (Exception e) { mode = false; } String value = xRootNode.Element(xNodeDoc).Value; StreamWriter destFile = new StreamWriter(filename, mode, System.Text.Encoding.Unicode); destFile.Write(value); destFile.Close(); return true; } catch (Exception e) { return false; } 

Does anyone have any ideas?

+6
source share
2 answers

If you want to save cr lf in the contents of an element or attribute when saving an XDocument or XElement , you can do this using specific XmlWriterSettings , namely NewLineHandling for Entitize:

  string fileName = "XmlOuputTest1.xml"; string attValue = "Line1.\r\nLine2."; string elementValue = "Line1.\r\nLine2.\r\nLine3."; XmlWriterSettings xws = new XmlWriterSettings(); xws.NewLineHandling = NewLineHandling.Entitize; XDocument doc = new XDocument(new XElement("root", new XAttribute("test", attValue), elementValue)); using (XmlWriter xw = XmlWriter.Create(fileName, xws)) { doc.Save(xw); } doc = XDocument.Load(fileName); Console.WriteLine("att value: {0}; element value: {1}.", attValue == doc.Root.Attribute("test").Value, elementValue == doc.Root.Value); 

In this example, the value is stored in a circular shutdown of saving and loading, since the sample output is "att: True, element value: True".

+3
source

Here is a useful link I found for parsing an Xml string with carraige returns, linear feeds in it.

howto-correctly-parse-using-xelementparse-for-strings-that-contain-newline-character-in

This can help those who parse the Xml string.

For those who can't be bothered with a click, he says use XmlTextReader instead

  XmlTextReader xtr = new XmlTextReader(new StringReader(xml)); XElement items = XElement.Load(xtr); foreach (string desc in items.Elements("Item").Select(i => (string)i.Attribute("Description"))) { Console.WriteLine("|{0}|", desc); } 
0
source

Source: https://habr.com/ru/post/903096/


All Articles