我期待在互聯網上如何保持從XML數據回車,但我無法找到答案,所以我在這裏:)如何保持回車從解析XML
的目標是在一個文件中寫入XML數據的內容。因此,如果節點的值包含某些「\ r \ n」數據,則軟件需要將它們寫入文件以創建新行,但它不會寫入,即使使用space:preserve。
這裏是我的測試類:
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());
這裏是我的過程類:
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;
}
沒有任何人有一個想法?
你應該這樣的數據放到<![CDATA [「」]]>因爲XML將嘗試解析。 – Burimi
如何做到這一點? – BaptX
它是xml解析器的工作,用於將不同的換行標準化爲\ n。所以這是默認行爲。 – Totonga