【问题标题】:C# - No colon symbol (":") allowed in XmlWriter?C# - XmlWriter 中不允许使用冒号 (":")?
【发布时间】:2016-10-16 06:38:35
【问题描述】:

我正在使用 C# 和 Mono 编写一个可以为 Apple macOS 官方词典应用程序生成词典的库。字典源代码是一个 XML 文档,它看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<!--
    This is a sample dictionary source file.
    It can be built using Dictionary Development Kit.
-->
<d:dictionary xmlns="http://www.w3.org/1999/xhtml" xmlns:d="http://www.apple.com/DTDs/DictionaryService-1.0.rng">
<d:entry id="dictionary_application" d:title="Dictionary application">
    <d:index d:value="Dictionary application"/>
    <h1>Dictionary application </h1>
    <p>
        An application to look up dictionary on Mac OS X.<br/>
    </p>
</d:entry></d:dictionary>

问题是,元素的本地名称包含一个冒号,我运行我的代码,当它转到 WriteStartElement 方法时会抛出一个异常并告诉我:

“d:dictionary”中的名称字符无效。 ':' 字符,十六进制值 0x3A,不能包含在名称中。

所以我想问一下,这个问题应该怎么解决,并在里面写上冒号的本地名称呢?

【问题讨论】:

标签: c# .net xml c#-4.0 mono


【解决方案1】:

注意元素本地名称是冒号后面的部分,例如d:dictionary 中的dictionary。冒号之前的部分是命名空间前缀。也就是说,您不想用冒号编写元素本地名称。您想改为使用命名空间前缀编写元素,这可以使用接受三个字符串参数的WriteStartElement() 的重载来完成:

string prefix = "d";
string localName = "dictionary";
string namespaceUri = "http://www.apple.com/DTDs/DictionaryService-1.0.rng";
writer.WriteStartElement(prefix, localName, namespaceUri);

参考:MSDN - XmlWriter.WriteStartElement(prefix, localName, namespace)

【讨论】:

    【解决方案2】:

    我更喜欢使用 xml linq,它是一个增强的网络库。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string header = 
                    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                    "<d:dictionary xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:d=\"http://www.apple.com/DTDs/DictionaryService-1.0.rng\">" +
                    "</d:dictionary>";
    
                XDocument doc = XDocument.Parse(header);
                XElement dictionary = (XElement)doc.FirstNode;
                XNamespace dNs = dictionary.GetNamespaceOfPrefix("d");
                XNamespace defaultNs = dictionary.GetDefaultNamespace();
    
                XElement newDict = new XElement(dNs + "entry", new object[] {
                    new XAttribute("id", "dictionary_application"),
                    new XAttribute("title","Dictionary application"),
                    new XElement(dNs + "index", new XAttribute("value", "Dictionary application")),
                    new XElement(defaultNs + "h1", "Dictionary application"),
                    new XElement(defaultNs + "p", "An application to look up in dictionary on Mac OS X.")
                });
    
                dictionary.Add(newDict);
    
            }
        }
    
    }
    

    【讨论】:

    • 我测试过你的程序输出不正确。行:
    • 但你的是:
    • 好的,我试过了,把代码new XElement(dNs + "index", new XAttribute("value", "Dictionary application")),改成new XElement(dNs + "index", new XAttribute(dNs + "value", "Dictionary application")),
    猜你喜欢
    • 2015-04-09
    • 2010-12-16
    • 2015-12-27
    • 2011-10-12
    • 2023-04-04
    • 1970-01-01
    • 2022-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多