Xml 在vb2008中用XSD元素和属性填充treeview
我正试图在VB2008项目中创建一个树视图,显示XML模式的所有元素和属性 XML模式的MSDN文档似乎暗示,通过将模式加载到XMLSchemaSet并编译它,我应该能够访问所有元素和属性及其值,但这在实践中似乎不起作用 使用for循环,例如:Xml 在vb2008中用XSD元素和属性填充treeview,xml,vb.net,xsd,Xml,Vb.net,Xsd,我正试图在VB2008项目中创建一个树视图,显示XML模式的所有元素和属性 XML模式的MSDN文档似乎暗示,通过将模式加载到XMLSchemaSet并编译它,我应该能够访问所有元素和属性及其值,但这在实践中似乎不起作用 使用for循环,例如: For Each elem As XmlSchemaElement In compiledSchema.Elements.Values 我可以使用elem.Name为简单类型获取元素名(以及具有类似嵌套循环的属性名),但这对复杂类型不起作用 无论何时(
For Each elem As XmlSchemaElement In compiledSchema.Elements.Values
我可以使用elem.Name
为简单类型获取元素名(以及具有类似嵌套循环的属性名),但这对复杂类型不起作用
无论何时(无论如何)我试图获取复杂类型的值,我都遇到了麻烦
例如,下面的模式只返回“bookstore”元素
'DisplayXmlFile-在树视图中加载XML文件
'在树视图中显示XML文件
'注意:需要导入System.Xml
'示例:DisplayXmlFile(“c:\employees.xml”,TreeView1)
私有子按钮2\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮2。单击
尝试
'Dim file_name As String=(“c:\employees.xml”)
TreeView1.Nodes.Clear()
DisplayXmlFile(“c:\satellites.xml”,TreeView1)
特例
结束尝试
端接头
'在树视图中显示XML文件
'注意:需要导入System.Xml
'示例:DisplayXmlFile(“employees.xml”,TreeView1)
子显示xmlfile(ByVal文件名为字符串,ByVal tvw为树视图)
Dim xmldoc作为新的XmlDocument()
加载(文件名)
'将其添加到TreeView节点集合
DisplayXmlNode(xmldoc,tvw.Nodes)
'展开根节点。
节点(0).Expand()
端接头
子显示xmlnode(ByVal xmlnode作为xmlnode,ByVal节点作为TreeNodeCollection)
'为此XmlNode添加TreeView节点。
(对于大多数XmlNode类型,使用节点名称是可以的。)
Dim tvNode As TreeNode=nodes.Add(xmlnode.Name)
选择Case xmlnode.NodeType
Case XmlNodeType.Element
'这是一个元素:检查是否有属性。
如果xmlnode.Attributes.Count>0,则
'创建属性节点。
Dim attrNode As TreeNode=tvNode.Nodes.Add(“(属性)”)
'将所有属性添加为新节点的子级。
Dim xmlAttr作为XmlAttribute
对于xmlnode.Attributes中的每个xmlAttr
'每个节点显示名称和值。
attrNode.Nodes.Add(xmlAttr.Name&“=”&xmlAttr.Value&_
"'")
下一个
如果结束
案例XmlNodeType.Text,XmlNodeType.CDATA
'对于这些节点类型,我们显示值
tvNode.Text=xmlnode.Value
Case XmlNodeType.Comment
tvNode.Text=“”
案例XmlNodeType.ProcessingInstruction,XmlNodeType.XmlDeclaration
tvNode.Text=“”
其他情况
'忽略其他节点类型。
结束选择
'为每个子节点递归调用此例程。
将xmlChild设置为XmlNode=XmlNode.FirstChild
直到孩子什么都不是
DisplayXmlNode(xmlChild、tvNode.Nodes)
xmlChild=xmlChild.NextSibling
环
端接头
这不适用于xml模式。无法仅返回元素名称字符串。
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.contoso.com/books" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="bookstore">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string" />
<xs:element name="author">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="name" type="xs:string" />
<xs:element minOccurs="0" name="first-name" type="xs:string" />
<xs:element minOccurs="0" name="last-name" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="price" type="xs:decimal" />
</xs:sequence>
<xs:attribute name="genre" type="xs:string" use="required" />
<xs:attribute name="publicationdate" type="xs:unsignedShort" use="required" />
<xs:attribute name="ISBN" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
bookstore
book
genre
publication date
isbn
title
author
first-name
last-name
price
'DisplayXmlFile - loading a XML file in a TreeView
' Display a XML file in a TreeView
' Note: requires Imports System.Xml
' Example: DisplayXmlFile("c:\employees.xml", TreeView1)
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
'Dim file_name As String = ("c:\employees.xml")
TreeView1.Nodes.Clear()
DisplayXmlFile("c:\satellites.xml", TreeView1)
Catch ex As Exception
End Try
End Sub
' Display a XML file in a TreeView
' Note: requires Imports System.Xml
' Example: DisplayXmlFile("employees.xml", TreeView1)
Sub DisplayXmlFile(ByVal filename As String, ByVal tvw As TreeView)
Dim xmldoc As New XmlDocument()
xmldoc.Load(filename)
' Add it to the TreeView Nodes collection
DisplayXmlNode(xmldoc, tvw.Nodes)
' Expand the root node.
tvw.Nodes(0).Expand()
End Sub
Sub DisplayXmlNode(ByVal xmlnode As XmlNode, ByVal nodes As TreeNodeCollection)
' Add a TreeView node for this XmlNode.
' (Using the node's Name is OK for most XmlNode types.)
Dim tvNode As TreeNode = nodes.Add(xmlnode.Name)
Select Case xmlnode.NodeType
Case XmlNodeType.Element
' This is an element: Check whether there are attributes.
If xmlnode.Attributes.Count > 0 Then
' Create an ATTRIBUTES node.
Dim attrNode As TreeNode = tvNode.Nodes.Add("(ATTRIBUTES)")
' Add all the attributes as children of the new node.
Dim xmlAttr As XmlAttribute
For Each xmlAttr In xmlnode.Attributes
' Each node shows name and value.
attrNode.Nodes.Add(xmlAttr.Name & " = '" & xmlAttr.Value & _
"'")
Next
End If
Case XmlNodeType.Text, XmlNodeType.CDATA
' For these node types we display the value
tvNode.Text = xmlnode.Value
Case XmlNodeType.Comment
tvNode.Text = "<!--" & xmlnode.Value & "-->"
Case XmlNodeType.ProcessingInstruction, XmlNodeType.XmlDeclaration
tvNode.Text = "<?" & xmlnode.Name & " " & xmlnode.Value & "?>"
Case Else
' ignore other node types.
End Select
' Call this routine recursively for each child node.
Dim xmlChild As XmlNode = xmlnode.FirstChild
Do Until xmlChild Is Nothing
DisplayXmlNode(xmlChild, tvNode.Nodes)
xmlChild = xmlChild.NextSibling
Loop
End Sub