如何用VisualBasic编写XML Schema实例?

时间:2016-01-14 11:10:50

标签: xml vb.net attributes

我正在用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">

但似乎&#34;:&#34; char不允许包含在属性中。 我该如何解决? 非常感谢你!

1 个答案:

答案 0 :(得分:1)

XML命名空间声明(例如xmlns:xsd="namespace_uri_here")与命名空间中的常见XML属性类似。该示例包含xmlns作为属性前缀,xsd作为属性local-name,"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
)