.net从数据库中读取数据保存为XML文件

.net从数据库中读取数据保存为XML文件,第1张

//创建xml

XmlDocument xmldoc = new XmlDocument()

//声明节

XmlDeclaration dec = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null)

xmldoc.AppendChild(dec)

//加入一个根节点

XmlElement oneNode = xmldoc.CreateElement("pricelist")

//创建节点

XmlElement twoNode = xmldoc.CreateElement("oilprices")

for(int i=0i<Table中的条数的大小i++)

{

XmlElement twoNodeone = xmldoc.CreateElement("price")

twoNodeone.SetAttribute("year", "year的值")

twoNodeone.SetAttribute("value", "表中的数据循环value的值")

twoNode.AppendChild(twoNodeone)//添加到oilprices节点下面

}

oneNode.AppendChild(twoNode)//添加到pricelist节点下面

//创建节点

XmlElement threeNode = xmldoc.CreateElement("fuelprices")

for (int i = 0i <Table中的条数的大小i++)

{

XmlElement threeNodeone = xmldoc.CreateElement("price")

threeNodeone.SetAttribute("year", "year的值")

threeNodeone.SetAttribute("e95", "表中的数据循环e95的值")

threeNodeone.SetAttribute("e98", "表中的数据循环e98的值")

threeNode.AppendChild(twoNodeone)//添加到fuelprices节点下面

}

oneNode.AppendChild(threeNode)//添加到pricelist节点下面

xmldoc.Save(Server.MapPath("")+"/1.xml")//保存xml

直接保存为xml:DataTable tab = new DataTable()tab.WriteXml("", XmlWriteMode.WriteSchema, true)

分类: 电脑/网络 >>程序设计 >>其他编程语言

解析:

System.Xml

XmlReader 抽象的读取器,提供快速、没有缓存的XML数据,XmlReader是只向前的,类似SAX分析器

XmlWriter 抽象类写入器,以流或文件的格式提供快速、没有缓存的XML数据

XmlTextReader 扩展XmlReader,提供访问XML数据的快速向前流

XmlTextWriter 扩展XmlWriter,快速生成只向前的XML流

XmlNode 抽象类,表示XML文档中一个节点的类,XML命名空间中几个类的基类

XmlDocument 扩展XmlNode,W3C DOM的实现,给出XML文档在内存中的树形表示,可以浏览和编辑它们

XmlDataDocument 扩展XmlDocument,即从XML数据中加载的文档,或从ADO.NET DataSet的关系数据中加载的文档,允许把XML和关系数据混合在同一个视图中

XmlResolver 抽象类,分析基于XML的外部资源,例如DTD的模式引用,也可以用于处理<xsl:include>和<xsl:import>元素

XmlUrlResolver 扩展XmlResolver,用URI解析外部资源

使用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 ("异常:{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

参考:http://chs.gotdotnet.com/quickstart/util/srcview.aspx?path=%2fquickstart%2fhowto%2fsamples%2fXml%2fWriteXmlFile%2fWriteXmlFile.src


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/6780083.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-28
下一篇2023-03-28

发表评论

登录后才能评论

评论列表(0条)

    保存