[英]Include XML CDATA in an element
更新:为每个请求添加了更多详细信息
我正在尝试为我的应用程序创建一个 xml 配置文件。 该文件包含要在 html 文档中搜索和替换的条件列表。 问题是,我需要搜索像 
这样的字符串。 我不希望我的代码读取解码后的项目,而是读取文本本身。
承认自己对 XML 很陌生,我确实做了一些尝试来满足这些要求。 我在 Stackoverflow 上阅读了大量关于CDATA
和ATTRIBUTES
等的链接,但这里(和其他地方)的示例似乎专注于在 xml 文件中创建一行,而不是多行。
这是我所做的许多尝试之一,但无济于事:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE item [
<!ELEMENT item (id, replacewith)>
<!ELEMENT id (#CDATA)>
<!ELEMENT replacewith (#CDATA)>
]>
]>
<item id=" " replacewith=" ">Non breaking space</item>
<item id="‑" replacewith="-">Non breaking hyphen</item>
这份文件给了我一些错误,包括:
<!ELEMENT id (#CDATA)>
。 在 CDATA 区域中,Visual Studio 通知我它需要一个“,”或“|”。]>
invalid token at the root of the document
给我一个invalid token at the root of the document
的错误。<item
条目之后,我收到一条错误消息,指出XML document cannot contain multiple root level elements
。如何编写包含多个项目的 xml 文件并允许我在元素中存储和检索文本,而不是解释的字符?
如果有帮助,我正在使用 .Net、C# 和 Visual Studio。
编辑:此 xml 文件的目的是为我的代码提供要在 html 文件中搜索和替换的内容列表。 该 xml 文件仅包含what to search for
和what to replace with
内容的列表。
这是我现在拥有的文件:
<?xml version="1.0" encoding="utf-8" ?>
<Items>
<item id="‑" replacewith="-">Non breaking hyphen</item>
<item id=" " replacewith=" ">Non breaking hyphen</item>
</Items>
以第一个为例,我想阅读文本‑
但当我阅读本文时,我明白了-
因为这就是代码所代表的意思。
您可以提供的任何帮助或指示都会有所帮助。
详细说明我的评论:由于保留字符,XML 的行为类似于 HTML。 当使用任何类型的解析器(浏览器、XML 阅读器等)读入时,与号前缀关键字或字符代码以转换为文字字符串。
对这些值进行转义以确保它们作为您想要的文字读回的最简单方法是将它们放入,就像您为 Web 对其进行编码一样。 例如,要创建您的 XML 文档,我是这样做的:
XmlDocument xmlDoc = new XmlDocument();
XmlElement xmlItem;
XmlAttribute xmlAttr;
XmlText xmlText;
// Declaration
XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlElement xmlRoot = xmlDoc.DocumentElement;
xmlDoc.InsertBefore(xmlDec, xmlRoot);
// Items
XmlElement xmlItems = xmlDoc.CreateElement(string.Empty, "Items", string.Empty);
xmlDoc.AppendChild(xmlItems);
// Item #1
xmlItem = xmlDoc.CreateElement(string.Empty, "item", string.Empty);
xmlAttr = xmlDoc.CreateAttribute(string.Empty, "id", string.Empty);
xmlAttr.Value = "‑";
xmlItem.Attributes.Append(xmlAttr);
xmlAttr = xmlDoc.CreateAttribute(string.Empty, "replacewith", string.Empty);
xmlAttr.Value = "-";
xmlItem.Attributes.Append(xmlAttr);
xmlText = xmlDoc.CreateTextNode("Non breaking hyphen");
xmlItem.AppendChild(xmlText);
xmlItems.AppendChild(xmlItem);
// Item #2
xmlItem = xmlDoc.CreateElement(string.Empty, "item", string.Empty);
xmlAttr = xmlDoc.CreateAttribute(string.Empty, "id", string.Empty);
xmlAttr.Value = " ";
xmlItem.Attributes.Append(xmlAttr);
xmlAttr = xmlDoc.CreateAttribute(string.Empty, "replacewith", string.Empty);
xmlAttr.Value = " ";
xmlItem.Attributes.Append(xmlAttr);
xmlText = xmlDoc.CreateTextNode("Non breaking hyphen");
xmlItem.AppendChild(xmlText);
xmlItems.AppendChild(xmlItem);
// For formatting
StringBuilder xmlBuilder = new StringBuilder();
XmlWriterSettings xmlSettings = new XmlWriterSettings
{
Indent = true,
IndentChars = " ",
NewLineChars = "\r\n",
NewLineHandling = NewLineHandling.Replace
};
using (XmlWriter writer = XmlWriter.Create(xmlBuilder, xmlSettings))
{
xmlDoc.Save(writer);
}
xmlOutput.Text = xmlBuilder.ToString();
请注意,我将您的id
值与您期望的值一起放入。 现在,看看它是如何编码的:
<?xml version="1.0" encoding="utf-16"?>
<Items>
<item id="&#8209;" replacewith="-">Non breaking hyphen</item>
<item id=" " replacewith="&nbsp;">Non breaking hyphen</item>
</Items>
你的和这个之间的唯一区别是&符号被编码为&
其余的保留为字符串文字。 这是 XML 的正常行为。 当你读回它时,它会以文字‑
返回‑
和
.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.