How to save newlines in XML attribute?

2019-01-03 16:03发布

I need to save content that containing newlines in some XML attributes, not text. The method should be picked so that I am able to decode it in XSLT 1.0/ESXLT/XSLT 2.0

What is the best encoding method?

Please suggest/give some ideas.

标签: xml xslt newline
3条回答
Juvenile、少年°
2楼-- · 2019-01-03 16:32

You can use the entity 
 to represent a newline in an XML attribute. 
 can be used to represent a carriage return. A windows style CRLF could be represented as 
.

This is legal XML syntax. See XML spec for more details.

查看更多
萌系小妹纸
3楼-- · 2019-01-03 16:46

A crude answer can be:

XmlDocument xDoc = new XmlDocument();
xDoc.Load(@"Agenda.xml");
//make stuff with the xml
//make attributes value = "\r\n" (you need both expressions to make a new line)
string a = xDoc.InnerXml.Replace("&#xD;", "\r").Replace("&#xA;", "\n").Replace("><",">\r    \n<");
StreamWriter sDoc = new StreamWriter(@"Agenda.xml");
sDoc.Write(a);
sDoc.Flush();
sDoc.Dispose();

This will as you see is just a string

查看更多
做个烂人
4楼-- · 2019-01-03 16:54

In a compliant DOM API there is nothing you need to do. Simply save actual newline characters to the attribute, the API will encode them correctly on its own (see Canonical XML spec, section 5.2).

If you do your own encoding (i.e. replacing \n with &#10; before saving the attribute value), the API will encode your input again, resulting in &amp;#10; in the XML file.

Bottom line is, the string value is saved verbatim. You get out what you put in, no need to interfere.

However… some implementations are not compliant. For example, they will encode & characters in attribute values, but forget about newline characters or tabs. This puts you in a losing position since you can't simply replace newlines with &#10; beforehand.

These implementations will save newline characters unencoded, like this:

<xml attribute="line 1
line 2" />

Upon parsing such a document, literal newlines in attributes are normalized into a single space (again, in accordance to the spec) - and thus they are lost.

Saving (and retaining!) newlines in attributes is impossible in these implementations.

查看更多
登录 后发表回答