vb.net与xml的简单介绍

vb.net中怎么创建xml文件并写数据DataSet 和 DataTable都有现成的方法:WriteXml
DataTable tb = this.dataGridView1.DataSource as DataTable;
if(tb != null)
{
tb.WriteXml(@"C:\table.xml",true);
return;
}
DataView dv =this.dataGridView1.DataSource as DataView;
if(dv != null)
{
dv.Table.WriteXml(@"C:\table.xml",true);
return;
}
IList list = this.dataGridView1.DataSource as IList;
if(list != null)
{
//to do,如果是IList,就要你自己想办法导出了
//XmlDocument or XmlWriter都可以考虑
}
VB.NET 有一个XML文件,如何读取它的指定内容你这不是一个标准的xml文件,所以建议用正则提?。?
Sub Main()
Dim xml = File.ReadAllText("C:\test\css.txt")
Dim rate = Regex.Match(xml, "rate([.\d]+)/rate").Groups(1).Value
Console.WriteLine(rate)
Console.ReadLine()
End Sub
vb.net操作xml数据库(急)使用System.XML
Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Xml
namespace HowTo.Samples.XML
public class WriteXmlFileSample
private const document as string = "newbooks.xml"
shared sub Main()
Dim myWriteXmlFileSample as WriteXmlFileSample
myWriteXmlFileSample = new WriteXmlFileSample()
myWriteXmlFileSample.Run(document)
end sub
public sub Run(args As String)
Dim myXmlTextReader as XmlTextReader = nothing
Dim myXmlTextWriter as XmlTextWriter = nothing
try
myXmlTextWriter = new XmlTextWriter (args, nothing)
myXmlTextWriter.Formatting = System.Xml.Formatting.Indented
myXmlTextWriter.WriteStartDocument(false)
myXmlTextWriter.WriteDocType("bookstore", nothing, "books.dtd", nothing)
myXmlTextWriter.WriteComment("此文件表示书店库存数据库的另一个片断")
myXmlTextWriter.WriteStartElement("bookstore")
myXmlTextWriter.WriteStartElement("book", nothing)
myXmlTextWriter.WriteAttributeString("genre","autobiography")
myXmlTextWriter.WriteAttributeString("publicationdate","1979")
myXmlTextWriter.WriteAttributeString("ISBN","0-7356-0562-9")
myXmlTextWriter.WriteElementString("title", nothing, "The Autobiography of Mark Twain")
myXmlTextWriter.WriteStartElement("Author", nothing)
myXmlTextWriter.WriteElementString("first-name", "Mark")
myXmlTextWriter.WriteElementString("last-name", "Twain")
myXmlTextWriter.WriteEndElement()
myXmlTextWriter.WriteElementString("price", "7.99")
myXmlTextWriter.WriteEndElement()
myXmlTextWriter.WriteEndElement()
'向文件写 XML 并关闭编写器
myXmlTextWriter.Flush()
myXmlTextWriter.Close()
' 读取返回的文件并进行分析以确保正确生成 XML
myXmlTextReader = new XmlTextReader (args)
FormatXml (myXmlTextReader, args)
catch e as Exception
Console.WriteLine ("异常vb.net与xml:{0}", e.ToString())
finally
Console.WriteLine()
Console.WriteLine("对文件 {0} 的处理已完成 。", args)
If Not myXmlTextReader Is Nothing
myXmlTextReader.Close()
end if
'关闭编写器
If Not myXmlTextWriter Is Nothing
myXmlTextWriter.Close()
end if
End try
End Sub
private shared Sub FormatXml (reader as XmlTextReader, filename as String)
Dim piCount, docCount, commentCount, elementCount as Integer
Dim attributeCount, textCount, whitespaceCount as Integer
While reader.Read()
Select (reader.NodeType)
case XmlNodeType.ProcessingInstruction:
Format (reader, "ProcessingInstruction")
piCount += 1
case XmlNodeType.DocumentType:
Format (reader, "DocumentType")
docCount += 1
case XmlNodeType.Comment:
Format (reader, "Comment")
commentCount += 1
case XmlNodeType.Element:
Format (reader, "Element")

推荐阅读