简体   繁体   English

在XML中存储文件名的正确方法是什么?

[英]What is the proper way to store a file name in XML?

I'm using XDocument to cache a list of files. 我正在使用XDocument来缓存文件列表。

<file id="20" size="244318208">a file with an &amp;ersand.txt</file>

In this example, I used XText , and let it automatically escape characters in the file name, such as the & with &amp; 在这个例子中,我使用了XText ,让它自动转义文件名中的字符,例如&with &amp;

<file id="20" size="244318208"><![CDATA[a file with an &ersand.txt]]></file>

In this one, I used XCData to let me use a literal string rather than an escaped one, so it appears in the XML as it would in my application. 在这一篇中,我使用XCData让我使用文字字符串而不是转义字符串,因此它在我的应用程序中出现在XML中。

I'm wondering if either of them is better than the other under any certain conditions, or if it is just personal taste. 我想知道他们中的任何一个在任何特定条件下是否比另一个更好,或者它是否仅仅是个人品味。 Also, if it means anything, the file names may or may not contain illegal characters. 此外,如果它意味着什么,文件名可能包含也可能不包含非法字符。

I wouldn't explicitly use either XText or XCData - I'd just provide a string and let LINQ to XML do whatever it wants. 我不会明确地使用XTextXCData - 我只是提供一个字符串,让LINQ to XML做任何想做的事情。

I do think the non-CDATA version is generally clearer though. 我认为非CDATA版本通常更清晰。 Yes, amperands are escaped - and < will be too - but that's still considerably less fluff than the CDATA start/end section. 是的,amperands是逃逸的 - 而且<也将 - 但是仍然比CDATA开始/结束部分少得多。

Don't forget that it should be pretty rare for humans to see the XML representation itself - the idea is that it's a transport for information which is reasonably readable in that representation when you need to . 不要忘记,人类很少看到XML表示本身 - 这个想法是它是一种信息传输, 在您需要时可以在该表示中合理地读取。 I wouldn't get too hung up about it. 我不会太在意这件事。

Both are essentially the same and there is no specific "best practice". 两者基本相同,没有具体的“最佳实践”。

Personally, I reserve <![CDATA[]]> for large amounts of text that requires lots of escaping (say bits of code or HTML markup). 就个人而言,我保留<![CDATA[]]>大量的文本,需要大量的转义(比如代码或HTML标记)。

In this specific case, I would rather escape the & to &amp; 在这种特殊情况下,我宁愿逃避&&amp; as in your first example. 就像你的第一个例子。

Most file names will not contain ampersands, or less then symbols. 大多数文件名不包含符号,或者不包含符号。 So go with XText. 所以请使用XText。 Reserve XCData for cases where you expect a lot of those characters, such as when embedding and HTML fragment in an XML document. 对于期望大量字符的情况,例如在XML文档中嵌入和HTML片段时,请保留XCData。

Rationale: difference in CPU utilization to serialize and parse text are completely negligible. 基本原理:序列化和解析文本的CPU利用率差异完全可以忽略不计。 But there is a (small) difference in storage, bandwidth or memory needs. 但存储,带宽或内存需求存在(小)差异。 Everything else being equal, use the format that uses the least space (even if the differences are small). 在其他条件相同的情况下,使用使用最小空间的格式(即使差异很小)。

It doesn't matter. 没关系。
They're both valid XML, and they both have the same meaning. 它们都是有效的XML,它们都具有相同的含义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM