You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET中使用XmlWriter添加noNamespaceSchemaLocation的问题

修正VB.NET代码生成正确的XML命名空间属性

你的问题出在调用WriteAttributeString写入xsi:noNamespaceSchemaLocation时,第三个参数(命名空间URI)传入了vbNull,导致XmlWriter无法识别已定义的xsi前缀,自动生成了默认前缀(比如p2)。

修正后的代码:

Dim writersettings As New XmlWriterSettings
Dim writer As XmlWriter
With writersettings
    .Indent = True
    .IndentChars = vbTab
    .NewLineChars = vbCrLf
    .NewLineHandling = NewLineHandling.Replace
    .Encoding = New UTF8Encoding(False)
End With
writer = XmlWriter.Create(outputName, writersettings)
writer.WriteStartDocument(True)
writer.WriteStartElement("CapCommsStatus")
' 定义xsi命名空间前缀
writer.WriteAttributeString("xmlns", "xsi", "http://www.w3.org/2000/xmlns/", "http://www.w3.org/2001/XMLSchema-instance")
' 写入xsi前缀的属性时,传入对应的命名空间URI
writer.WriteAttributeString("xsi", "noNamespaceSchemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "CapCommsStatus_v1.xsd")
' 确保文档结构完整
writer.WriteEndElement()
writer.WriteEndDocument()
writer.Close()

关键说明:

  • 写入xsi:noNamespaceSchemaLocation时,第三个参数必须指定xsi对应的命名空间URI:http://www.w3.org/2001/XMLSchema-instance,这样XmlWriter才会关联之前定义的xsi前缀,避免生成新的默认前缀。
  • 补充了收尾方法,保证XML文档结构闭合完整。

内容的提问来源于stack exchange,提问作者Colster

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 00:25:57