【问题标题】:Problems to write xmlns entries with xmlwriter使用 xmlwriter 编写 xmlns 条目的问题
【发布时间】:2019-02-08 16:08:43
【问题描述】:

我的 .xml 文件中正好需要这个标头:

<document xsi:schemaLocation="http://www.xxxxx.ch/schema/2.1 
http://www.xxxxx.ch/schema/xxxxx_2.1.01.xsd" 
xmlns="http://www.xxxxx.ch/schema/2.1" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

我无法根据需要创建文件,因为 xmlwriter 在我的所有尝试中都抛出了错误(从一整天以来我尝试了所有我发现的东西)。
这里缺少什么 - 如何根据需要生成文件? 代码截断:

Dim settings As XmlWriterSettings = New XmlWriterSettings()
settings.Indent = True
settings.CloseOutput = True
Dim cFileExport As String = cTempAblagenPfad + cFilenameExport_XML
Using writer As XmlWriter = XmlWriter.Create(cFileExport, settings)
writer.WriteStartDocument()
'
writer.WriteStartElement("document")
writer.WriteAttributeString("xsi", "schemaLocation", vbNull, "http://www.xxxxx.ch/schema/2.1 http://www.xxxxx.ch/schema/xxxxx_2.1.01.xsd")
' Generates XML: <document xsi:schemaLocation="http://www.xxxxx.ch/schema/2.1 http://www.xxxxx.ch/schema/xxxxx_2.1.01.xsd" xmlns:xsi="1">
' I have no idea, why xmlns:xsi="1" is generated by the xml-writer - maybe that’s the problem?
writer.WriteAttributeString("xmlns", "http://www.xxxxx.ch/schema/2.1", XmlSchema.InstanceNamespace) 
' throws: "The prefix "xmlns" is reserved for XML
writer.WriteAttributeString("xmlns", "http://www.xxxxx.ch/schema/2.1") 
' throws: "the prefix '' cannot be changed from '' to 'http://www.xxxxx.ch/schema/2.1' in the same startelement tag"
 writer.WriteAttributeString("xmlns", "xsi", vbNull, "http://www.w3.org/2001/XMLSchema-instance")
‘throws: "The prefix "xmlns" is reserved for XML
....

感谢任何提示..

【问题讨论】:

  • 例外?你的问题没有例外。或许您应该 edit 并将它们包括在内,这样人们就可以看到出了什么问题。
  • 我已将文本中的“异常”更改为“错误” - 错误消息在代码中以 cmets 的形式出现(从德语翻译成英语)

标签: xml vb.net visual-studio-2017 xmlwriter


【解决方案1】:

经过多次尝试和错误,我自己找到了解决方案:

代码:

Dim cNameSpace As String = "http://www.xxxxx.ch/schema/2.1"
Dim cSchemaInstance As String = "http://www.w3.org/2001/XMLSchema-instance"
'
Using writer As XmlWriter = XmlWriter.Create(cFileExport, settings)
  writer.WriteStartDocument()
  writer.WriteStartElement("document", cNameSpace) ------
  writer.WriteAttributeString("xmlns", "xsi", "http://www.w3.org/2000/xmlns/", cSchemaInstance)
  writer.WriteAttributeString("xsi", "schemaLocation", cSchemaInstance, "http://www.xxxxx.ch/schema/2.1 http://www.xxxxx.ch/schema/xxxxx_2.1.01.xsd")

生成:

<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:schemaLocation="http://www.xxxxx.ch/schema/2.1  
http://www.xxxxx.ch/schema/eSchKG_2.1.01.xsd"  
xmlns="http://www.xxxxx.ch/schema/2.1">

与示例中的顺序不完全一样,但这无关紧要...
该文件现在被系统接受为有效,我必须将文件发送到那里。

【讨论】:

    猜你喜欢
    • 2011-03-15
    • 2010-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多