VB NET与XML之间的操作

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Imports System.Xml'声明XML

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles Button1.Click

Dim dataset As New DataSet'声明一个dataset

dataset.ReadXml("f:¥aaa.xml")'读xml文件,并把xml文件放入到datset的第一个表中

Dim table As DataTable=dataset.Tables(0)'读出该表

Dim view As DataView=New DataView(table)'自定义视图

DataGridView1.DataSource=view'绑定数据源

End Sub

Public Sub appear()'同上

Dim dataset As New DataSet

dataset.ReadXml("f:¥aaa.xml")

Dim table As DataTable=dataset.Tables(0)

Dim view As DataView=New DataView(table)

DataGridView2.DataSource=view

End Sub

Private Sub Button2_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles Button2.Click

Dim xmlDou As XmlDocument=New XmlDocument()'声明一个xml文档对象

xmlDou.Load("f:¥aaa.xml")'将特定的xml文档写入xmldoucument

Dim xmlnode As XmlNode=xmlDou.SelectSingleNode("bookstore")'声明一个根标记,bookstore为根标记

Dim xml1As XmlElement=xmlDou.CreateElement("book")'声明一个xml 的节点

xml1.SetAttribute("genre","李簪红")'节点的属性

xml1.SetAttribute("ISBN","2-3621-4")'同上

Dim xml1_title As XmlElement=xmlDou.CreateElement("title")'声明一个xml的节点

xml1_title.InnerText="C#程序设计入门与技巧"'定义节点的文档

xml1.AppendChild(xml1_title)'向总节点尾部添加节点

Dim xml1_author As XmlElement=xmlDou.CreateElement("author")'同上xml1_author.InnerText="张永斌"

xml1.AppendChild(xml1_author)

Dim xml1_price As XmlElement=xmlDou.CreateElement("price")

xml1_price.InnerText="56.7"

xml1.AppendChild(xml1_price)

xmlnode.AppendChild(xml1)

Dim xml2As XmlElement=xmlDou.CreateElement("book")

xml2.SetAttribute("genre","乐于")

xml2.SetAttribute("ISBN","3-3423-5")

Dim xml2_title As XmlElement=xmlDou.CreateElement("title")

xml2_title.InnerText="JAVA"

xml2.AppendChild(xml2_title)

Dim xml2_author As XmlElement=xmlDou.CreateElement("author")

xml2_author.InnerText="防御忧郁"

xml2.AppendChild(xml2_author)

Dim xml2_price As XmlElement=xmlDou.CreateElement("price")

xml2_price.InnerText="44.4"

xml2.AppendChild(xml2_price)

xmlnode.AppendChild(xml2)

xmlDou.Save("f:¥aaa.xml")

appear()

End Sub

Private Sub Button3_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles Button3.Click

Dim xmlDoc As XmlDocument=New XmlDocument'同上

xmlDoc.Load("f:¥aaa.xml")'同上

Dim nodelist As XmlNodeList=

xmlDoc.SelectSingleNode("bookstore").ChildNodes()'定义了一个xmlnodelist用于放根节点为bookstore的所有字标记

For Each node As XmlNode In nodelist'遍历整个xmlnodelist

Dim elem As XmlElement=node'将节点转换为element元素

Dim elem_children As XmlNodeList=elem.ChildNodes()'定义了一个elem_children用于放根节点为bookstore的所有字标记

If elem.GetAttribute("genre")="李簪红"Then'获得第一子节点的属性elem.SetAttribute("genre","updata李簪红")'更新第一节点的属性End If

For Each elem_node As XmlNode In elem_children'遍历第二子节点

相关文档
最新文档