微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在XML属性上使用冒号错误

如何解决在XML属性上使用冒号错误

| 如何使用XDocument类并将其属性名称设置为接受冒号字符?我得到这个错误   \“名称中不能包含\':\'字符,十六进制值0x3A。
Dim ns As XNamespace = \"http://www.sitemaps.org/schemas/sitemap/0.9\"
Dim xi As XNamespace = \"http://www.w3.org/2001/XMLSchema-instance\"

Dim sitemapValue As New XDocument(New XDeclaration(\"1.0\",\"utf-8\",\"\"),New XElement(\"urlset\",New XAttribute(\"xmls\",ns),New XAttribute(\"xmls:xi\",xi)))
我只是想在下面使用XDocument类输出以下标头。
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<urlset
      xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"
      xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
      xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9
            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">
    

解决方法

尝试(使用VS 2010,否则您需要添加换行符)
Dim ns As XNamespace = \"http://www.sitemaps.org/schemas/sitemap/0.9\"
Dim xi As XNamespace = \"http://www.w3.org/2001/XMLSchema-instance\"

Dim doc As XDocument = New XDocument(
                       New XElement(ns + \"urlset\",New XAttribute(XNamespace.Xmlns + \"xsi\",xi),New XAttribute(xi + \"schemaLocation\",\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\")))
    ,
Dim ns As XNamespace = \"http://www.sitemaps.org/schemas/sitemap/0.9\"
Dim xi As XNamespace = \"http://www.w3.org/2001/XMLSchema-instance\"

Dim sitemapValue As New XDocument(New XDeclaration(\"1.0\",\"utf-8\",\"\"),New XElement(\"urlset\",New XAttribute(\"xmls\",ns),_
                                     New XAttribute(XNamespace.Xmlns + \"xi\",\"http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\")))
输出:
<urlset xmls=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xi=\"http://www.w3.org/2001/XMLSchema-instance\" xi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\" />
让我知道这是否是您要的。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。