Проблемы записи записей xmlns с помощью xmlwriter - PullRequest
0 голосов
/ 08 февраля 2019

Мне нужен именно этот заголовок в моих .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
....

Спасибо за любую подсказку ..

1 Ответ

0 голосов
/ 12 февраля 2019

После долгих проб и ошибок я сам нашел решение:

Код:

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

Неточно такой же порядок, как в примере, но это не имеет значения ...
Файл был теперь принят как действительный из системы, куда я должен отправить файл.

...