符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
使用System.XML
创新互联从2013年成立,是专业互联网技术服务公司,拥有项目网站制作、做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元曲周做网站,已为上家服务,为曲周各地企业和个人服务,联系电话:028-86922220
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 ("异常:{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")
elementCount += 1
While reader.MoveToNextAttribute()
Format (reader, "Attribute")
end While
if (reader.HasAttributes)
attributeCount += reader.AttributeCount
end if
case XmlNodeType.Text:
Format (reader, "Text")
textCount += 1
case XmlNodeType.Whitespace:
whitespaceCount += 1
End Select
End While
' 显示该文件的统计信息
Console.WriteLine ()
Console.WriteLine("{0} 文件的统计信息", filename)
Console.WriteLine ()
Console.WriteLine("处理指令:" piCount)
Console.WriteLine("文档类型:" docCount)
Console.WriteLine("注释:" commentCount)
Console.WriteLine("元素:" elementCount)
Console.WriteLine("属性:" attributeCount)
Console.WriteLine("文本:" textCount)
Console.WriteLine("空白:" whitespaceCount)
End Sub
private shared Sub Format(byref reader as XmlTextReader , NodeType as String)
' 格式化输出
Console.Write(reader.Depth " ")
Console.Write(reader.AttributeCount " ")
Dim i as Integer
for i = 0 to reader.Depth - 1
Console.Write(Strings.chr(9))
Next
Console.Write(reader.Prefix NodeType "" reader.Name "" reader.Value)
Console.WriteLine()
End Sub
End Class
End Namespace
参考:
使用DataSet类来完成这项任务。
DataSet对象在传输时,是以XML流的形式而不是以COM形式传输的。DataSet对象可以读取XML数据文件或者数据流,从而将树型结构的XML数据转换成关系型的数据,如表(DataTable)、列(DataColumn)、行(DataRow)等。
在这个实例中,我们将XML文件命名为“xmlfile.xml”,具体内容如下:
?xml version=\"1.0\"standalone=”yes”?
Detail
Person
NameManish/Name
Age22/Age
/Person
/Detail \'用VB.NET读写XML文件
C1aSS WriteXML
Shared Sub main()
Dim obj DataSet As New System.Data.DataSet()
Dim strVirtualPath As String=”t.xml”
\'载入XML文件DataSet
objDataSet.ReadXml(”xmlfile.xml”)
\'通过控制器读取XML内容
Console.Write(objDataSet.GetXml)
\'从原XML文件中得到数据
ConSOle.Write(”Enter Name:”)
Dim fname,age As String
fname=ConS01e.ReadLine
\'写入你希望的代码
ConS01e.Write(”Enter Age:”)
age=Console.ReadLine
ConS0le.Write(fname&age)
Dim v(1)As String
v(0)=fname
V(1)=age
\'增加数据DataSet
objDataSet.Tables(0).Rows.Add(V)
\'更新XML文件
objDataSet.WriteXml(”xmlfile.xml”)
Console.Write(obj DataSet.GetXml)
End Sub
End C1ass
运行该程序有两种方式,一种是在Microso~VS.NET框架中编译完后运行;另一种则是在仿DOS的Command窗口中运行。在第一种方式中,必须加上System、System.Data、System.xML的引用。具体方法为:先创建一个空的项目,然后加入这个文件名字;在“解决方案资源管理器”中加入引用,此时需要选择相应的.dll文件;注意,xmlfile.xml文件应该放在工程的/bin目录下面。我们可以输出结果也可以用浏览器直接查看改变后的XML文件。在第二种方式中,选择桌面的“程序”一“Microso~Visual Studio.NET”一“VisualStudio.NET工具”一“Visual Studio.NET命令提示”命令,在DOS命令提示框敲入命令:[Page]
vbc/r:system.d11/r:system.data.d11/r:system.xml.d11 xml.vb其中,程序名为xml.vb,这里的3个.dll文件都是必须调用的库文件。应该在存放VB.NET文件的目录中运行上述命令,否则找不到相应的程序,当然设置PATH也可以做到。此外,Xml_vb文件应该和XML文件放在同一目录下面。每次运行此程序都会往文件中写进同样的内容,都是写入Person/,运行一次写一行。其实,我们只要稍微修改一下这个程序,就可以往XML中写入我们所需要的数据。
Dim xmlDoc As New System.Xml.XmlDocument
xmlDoc.Load("c:\xml.xml") '载入xml文件
Dim Items As Xml.XmlNodeList = xmlDoc.DocumentElement.SelectNodes("//record/item") '参数为xpath查询串,前面斜杠,//:表示任何结点,/:表示根结点
For Each s As Xml.XmlNode In Items
Console.WriteLine(s.Attributes.GetNamedItem("id").Value vbTab s.InnerText)
Next