2016-01-14 21 views
2

我正在做我的第一個項目與VisualBasic。包括編寫XML並保存。這是我的代碼:如何使用VisualBasic編寫XML模式實例?

Imports System 
Imports System.Xml 
Public Class Form1 

Private Sub saveXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveXML.Click 
    Dim settings As New XmlWriterSettings() 
    settings.Indent = True 


    Dim XmlWrt As XmlWriter = XmlWriter.Create("C:\Documents and Settings\dpradell\MyName.xml", settings) 

    With XmlWrt 


     .WriteStartDocument() 
     .WriteAttributeString("xmlns:xsd", "http://www.w3.org/2001/XMLSchema") 
     .WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") 

而與此部分:

 .WriteStartElement("Production") 
     .WriteAttributeString("xmlns:xsd", "http://www.w3.org/2001/XMLSchema") 
     .WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") 

我想寫這個聲明:

<Production xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

但是好像 「:」 字符不允許是包含在屬性中。 我該如何解決它? 非常感謝!

回答

1

XML名稱空間聲明(例如xmlns:xsd="namespace_uri_here")類似於名稱空間中的常見XML屬性。示例由xmlns作爲屬性前綴,xsd作爲屬性本地名稱,"namespace_uri_here"作爲屬性值組成。要編寫使用XmlWriter這樣的描述屬性,你可以這樣說:

.... 
XmlWrt.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema") 

這裏是正在使用的方法的簽名上面(見MSDN細節):

Public Sub WriteAttributeString (
    prefix As String, 
    localName As String, 
    ns As String, 
    value As String 
) 
+0

謝謝!工作正常 –