【问题标题】:Error using colon character on XML attributes在 XML 属性上使用冒号字符时出错
【发布时间】:2011-05-23 06:01:53
【问题描述】:

如何使用 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">

【问题讨论】:

    标签: .net vb.net linq linq-to-xml


    【解决方案1】:

    试试(用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")))
    

    【讨论】:

      【解决方案2】:
      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" />
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-26
        • 2018-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-18
        • 1970-01-01
        • 2022-11-23
        相关资源
        最近更新 更多