.net - 在 XML 属性上使用冒号字符时出错

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

如何使用 XDocument 类并将其属性名称设置为接受冒号字符?我收到此错误

"The ':' character, hexadecimal value 0x3A, cannot be included in a name."

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 类得到以下 header 输出。

<?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")))

关于.net - 在 XML 属性上使用冒号字符时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6093387/

相关文章:

c# - 如何根据 Entity Framework 中的上下文验证实体?

.net - ASP.NET WebAPI 中的模拟和异步

c# - 监听NSTableView的事件防止选择和列拖动

c# - 使用 Linq 和 Lambda 包含默认值

c# - ASP 网络核心 : add many to many relationship with IdentityUser

vb.net - 从文件夹加载图像 [VB.NET]

c# - 您如何随机将整数中的一位归零?

c# - 除了 Replace() 函数,还有其他替换方法吗?

c# - 如何测试 IEnumerable<int>

c# - Lambda 表达式为列表中的每个不同值返回一个结果