使用 xmlwriter 写入 xmlns 条目的问题
Problems to write xmlns entries with xmlwriter
我的 .xml-files:
中正好需要这个 header
<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
....
感谢任何提示..
经过大量的尝试和错误,我自己找到了解决方案:
代码:
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">
与示例中的顺序不完全相同,但这并不重要...
该文件现在被系统接受为有效,我必须将文件发送到那里。
我的 .xml-files:
中正好需要这个 header<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
....
感谢任何提示..
经过大量的尝试和错误,我自己找到了解决方案:
代码:
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">
与示例中的顺序不完全相同,但这并不重要...
该文件现在被系统接受为有效,我必须将文件发送到那里。