使用xmlwriter编写xmlns条目的问题

问题描述 投票:0回答:1

我需要在我的.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
....

谢谢你的任何提示..

xml vb.net visual-studio-2017 xmlwriter
1个回答
0
投票

经过大量的尝试和错误,我自己找到了解决方案:

编码:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.