Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How keep carriage return from parsing XML

Tags:

I am looking on Internet how keep the carriage return from XML data but I could not find the answer, so I'm here :)

The objective is to write in a file the content of a XML data. So, if the value of the node contains some "\r\n" data, the soft need to write them in file in order to create new line, but it doesn't write, even with space:preserve.

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 anybody have an idea?