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
//创建xmlXmlDocument 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)
sql语句的时候把所有的数据进行串联成字符串。然后进行压缩。然后接收到之后按照压缩对应的解压缩方式进行解压缩。
提前封装一个xml读写类,利用xml读写类进行插入生产xml文件。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)