using (System.IO.StreamReader sr = new System.IO.StreamReader(path, System.Text.Encoding.GetEncoding("Windows-1252"), true))
{
xdoc = XDocument.Load(sr);
}
Here is my XML
<sheet name="sheet1" bounds="160,128,464,288">
<text name="text1" bounds="160,128,464,288" text="a
b"/>
</sheet>
XDocument.Load converts
a
b
to
a b
How to preserve my line breaks?
Whitespaces in attributes are normalized by default to be converted to spaces. It is much safer to have text with new lines in elements instead of attributes.
If it is out of your control - setting XmlTextReader.Normalization to false
should prevent default behavior.
Partial sample from the article below:
// Create the XML fragment to be parsed.
string xmlFrag =
@"<item attr1=' test A B C
1 2 3'/>
<item attr2=''/>";
XmlTextReader reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context);
// Show attribute value normalization.
reader.Read();
reader.Normalization = false;
Console.WriteLine("Attribute value:{0}", reader.GetAttribute("attr1"));
reader.Normalization = true;
Console.WriteLine("Attribute value:{0}", reader.GetAttribute("attr1"));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.