在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", xi), New XAttribute(xi + "schemaLocation", "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" />

如果这是你想要的,请告诉我。

相关内容

  • 没有找到相关文章

最新更新