【问题标题】:Include XML CDATA in an element在元素中包含 XML CDATA
【发布时间】:2016-04-20 01:35:14
【问题描述】:

更新:为每个请求添加了更多详细信息

我正在尝试为我的应用程序创建一个 xml 配置文件。该文件包含要在 html 文档中搜索和替换的条件列表。问题是,我需要搜索像&nbsp 这样的字符串。我不希望我的代码读取解码的项目,而是读取文本本身。

承认我对 XML 还很陌生,但我确实为满足要求做了一些尝试。我在 Stackoverflow 上阅读了大量关于 CDATAATTRIBUTES 等的链接,但这里(和其他地方)的示例似乎专注于在 xml 文件中创建单行,而不是多行。

这是我做了很多无济于事的尝试之一:

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE item [
  <!ELEMENT item (id, replacewith)>
  <!ELEMENT id (#CDATA)>
  <!ELEMENT replacewith (#CDATA)>
  ]>
]>
<item id=" " replacewith="&nbsp;">Non breaking space</item>
<item id="&#8209;" replacewith="-">Non breaking hyphen</item>

这份文件给了我一些错误,包括:

  • 在 DOCTYPE 中,出现&lt;!ELEMENT id (#CDATA)&gt; 之类的错误。在 CDATA 区域中,Visual Studio 通知我它需要一个“,”或“|”。
  • ]&gt; 给我一个错误 invalid token at the root of the document
  • 当然,在第二个 &lt;item 条目之后,我收到一条错误消息,指出 XML document cannot contain multiple root level elements

如何编写包含多个项目的 xml 文件并且允许我存储和检索元素内的文本,而不是解释的字符?

如果有帮助的话,我正在使用 .Net、C# 和 Visual Studio。

编辑: 此 xml 文件的目的是为我的代码提供要在 html 文件中搜索和替换的内容列表。 xml 文件只包含what to search forwhat to replace with 的列表。

这是我现在的文件:

<?xml version="1.0" encoding="utf-8" ?>
<Items>
  <item id="&#8209;" replacewith="-">Non breaking hyphen</item>
  <item id=" " replacewith="&nbsp;">Non breaking hyphen</item>
</Items>

以第一个为例,我想阅读文本&amp;#8209,但当我阅读此内容时,却得到-,因为这就是代码所代表的内容。

您可以提供的任何帮助或建议都会有所帮助。

【问题讨论】:

  • 不清楚您想要达到的目标。您想查看的 valid XML 示例会有所帮助。如果您尝试使用字符串操作/正则表达式读取/写入 XML - 请停止这样做(或者至少不要在 SO 上询问它)。
  • 我已经更新了更详细的描述和我正在使用的 xml 文件。基本上,我正在尝试编译要在 html 文件中搜索和替换的字符串列表。这些字符串可由用户配置,所以我想我会将它们存储在一个 xml 配置文件中。我基本上想将属性中包含的文本作为原始字符串而不是解码字符串来读取。
  • 如果您想要文本,请对其进行编码。例如,NBSP 将是 &amp;amp;nbsp;
  • @Bill - 这种编码对你有用吗?我还在我的回答中添加了一个指向实体列表的链接,以防它对您有价值。

标签: c# xml visual-studio


【解决方案1】:

详细说明我的评论:由于保留字符,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 = "&#8209;";
     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 = "&nbsp;";
     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="&amp;#8209;" replacewith="-">Non breaking hyphen</item>
  <item id=" " replacewith="&amp;nbsp;">Non breaking hyphen</item>
</Items>

你的和这个唯一的区别是和号被编码为&amp;amp;,其余的仍然是字符串文字。这是 XML 的正常行为。当你读回它时,它会以文字 &amp;amp;#8209;&amp;nbsp; 的形式返回。

【讨论】:

  • 这种方法也适用于其他保留字符,例如&amp;lt; 更改为&amp;lt;&amp;gt; 更改为&amp;gt;(反之亦然)。如需更多实体乐趣,请查看:dev.w3.org/html5/html-author/charref
  • 嗨,克里斯。谢谢你提供这个。我今天会测试。我认为这里的主要区别在于您想出了一种方法来创建一个简单的 XML 文档并存储信息,而我试图强制 XML 将文本视为文字字符串。在这一点上,我对任何可行的解决方案持开放态度:-)
  • 我只是展示了当您生成 XML 文档时,它会自动正确地对其进行编码。如果您要手动完成,则需要手动对其进行编码,例如&amp;amp;#8209;。基本上,只需用 &amp;amp; 替换每个属性中的第一个与号实例
  • 如果您使用任何类型的 XML 阅读器来检索 &amp;amp;#8209; 的属性值,它将以 &amp;amp;#8209; 的形式返回
  • 谢谢。我的代码处于测试阶段,所以我只是让用户通过 UI 修改 XML。我喜欢你的方法,但显然需要优先考虑 UI 实现以避免他们输入错误。由于我的用户不是 XML 专家,这已经有点令人困惑了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-27
  • 2012-05-02
  • 2016-09-24
  • 1970-01-01
  • 2013-11-26
  • 1970-01-01
  • 2011-07-06
相关资源
最近更新 更多