Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在元素中包含XML CDATA_C#_Xml_Visual Studio - Fatal编程技术网

C# 在元素中包含XML CDATA

C# 在元素中包含XML CDATA,c#,xml,visual-studio,C#,Xml,Visual Studio,更新:为每个请求添加更多详细信息 我正在尝试为我的应用程序创建一个xml配置文件。该文件包含要在html文档中搜索和替换的条件列表。问题是,我需要搜索字符串,如。我不希望我的代码读取解码的项目,但文本本身 我承认自己对XML非常陌生,我确实在满足需求方面做了一些尝试。我在Stackoverflow上看到了大量关于CDATA和属性等的链接,但这里(和其他地方)的示例似乎侧重于在xml文件中创建一行,而不是多行 以下是我所做的许多徒劳尝试之一: <?xml version="1.0" enco

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

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

我承认自己对XML非常陌生,我确实在满足需求方面做了一些尝试。我在Stackoverflow上看到了大量关于
CDATA
属性
等的链接,但这里(和其他地方)的示例似乎侧重于在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>
以第一个为例,我想阅读文本
,但当我阅读此文本时,我得到了
-
,因为这就是代码所代表的内容


您能提供的任何帮助或指示都会很有帮助。

详细说明我的评论:由于保留字符,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>

不间断连字符
不间断连字符

您的与此之间的唯一区别在于,与的编码为
&,其余部分保留为字符串文本。这是XML的正常行为。当您读回它时,它将以文本形式返回

不清楚您试图实现的目标。您希望看到的有效XML示例将有所帮助。如果您正在尝试使用字符串操作/regex读取/写入XML,请停止这样做(或者至少不要在so上询问)。我已经更新了更详细的描述和我正在使用的XML文件。基本上,我正在尝试编译一个字符串列表,我想在html文件中搜索并替换这些字符串。这些字符串可以由用户配置,所以我想我应该将它们存储在一个xml配置文件中。我基本上希望将属性中包含的文本作为原始字符串而不是解码字符串来读取。如果需要文本,请对其进行编码。例如,NBSP将是
&;nbsp@Bill-这个编码对你有用吗?我还在我的答案中添加了一个实体列表链接,以防它对您有价值。这种方法也适用于其他保留字符,例如
改为
(反之亦然)。想获得更多的乐趣,请看这个:嗨,克里斯。谢谢你提供这个。我今天要考试。我认为这里的主要区别在于,您已经找到了一种方法来创建一个简单的XML文档并存储信息,而我试图强制XML将文本视为文本字符串。在这一点上,我对任何有效的解决方案都持开放态度:-)我只是告诉大家,当您生成一个XML文档时,它会自动正确编码。如果要手动执行,则需要手动编码,例如
&#8209;。基本上,只需将每个属性中的第一个与符号实例替换为
&
如果使用任何类型的XML读取器来检索
的属性值&#8209;,它将返回为
谢谢。我的代码是beta版的,所以我只是让用户通过UI修改XML。我喜欢您的方法,但显然需要首先确定UI实现的优先级,以避免他们键入错误的内容。这已经有点令人困惑了,因为我的用户不是XML专家。
<?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>