asp.net 操作XML 按指定格式写入XML数据 WriteXml_实用技巧

复制代码 代码如下:

Private Sub WriteXml()
Try
Create(strFName, "NewDataSet")
SaveXMLFile("gg", "NewDataSet", True)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Function Create(ByVal FileName As String, ByVal Root As String) As Boolean
Dim NewXML As XmlTextWriter = Nothing
Irowcount = objDataSet.Tables("DataAnalyseVIEW").Rows.Count
Icolcount = objDataSet.Tables("DataAnalyseVIEW").Columns.Count
Try
NewXML = New XmlTextWriter(FileName, Nothing)
NewXML.Formatting = Formatting.Indented
NewXML.WriteStartDocument()
NewXML.WriteComment(My.Application.Info.AssemblyName & " Settings ") '项目名称
NewXML.WriteStartElement(Root)
NewXML.WriteStartElement(objDataSet.Tables("DataAnalyseVIEW").TableName.ToString())
NewXML.WriteStartElement(objDataSet.Tables("DataAnalyseVIEW").Columns(2).ColumnName().ToString())
NewXML.WriteString(objDataSet.Tables("SerialNumberTable").Rows(txtN)("SerialNumber").ToString())
NewXML.WriteEndElement()
NewXML.WriteStartElement(objDataSet.Tables("DataAnalyseVIEW").Columns(3).ColumnName().ToString())
NewXML.WriteString(objDataSet.Tables("DataAnalyseVIEW").Rows(0)("Client").ToString())
NewXML.WriteEndElement()
NewXML.WriteStartElement(objDataSet.Tables("DataAnalyseVIEW").Columns(5).ColumnName().ToString())
NewXML.WriteString(objDataSet.Tables("DataAnalyseVIEW").Rows(0)("Productor").ToString())
NewXML.WriteEndElement()
NewXML.WriteStartElement(objDataSet.Tables("DataAnalyseVIEW").Columns(6).ColumnName().ToString())
NewXML.WriteString(objDataSet.Tables("DataAnalyseVIEW").Rows(0)("Model").ToString())
NewXML.WriteEndElement()
NewXML.WriteStartElement(objDataSet.Tables("DataAnalyseVIEW").Columns(4).ColumnName().ToString())
NewXML.WriteString(objDataSet.Tables("DataAnalyseVIEW").Rows(0)("StationName").ToString())
NewXML.WriteEndElement()
NewXML.WriteStartElement(objDataSet.Tables("DataAnalyseVIEW").Columns(7).ColumnName().ToString())
NewXML.WriteString(objDataSet.Tables("DataAnalyseVIEW").Rows(0)("DataTime").ToString())
NewXML.WriteEndElement()
NewXML.WriteStartElement(objDataSet.Tables("DataAnalyseVIEW").Columns(8).ColumnName().ToString())
NewXML.WriteString(objDataSet.Tables("DataAnalyseVIEW").Rows(0)("status").ToString())
NewXML.WriteEndElement()
NewXML.WriteEndElement()
Progressbar1.Step = 1
Progressbar1.Value = 1
Progressbar1.Visible = True
For Irow = 0 To Irowcount - 1
Progress(Irowcount - 1)
NewXML.WriteStartElement(objDataSet.Tables("DataAnalyseVIEW").TableName.ToString())
For Icol = 0 To Icolcount - 1
If Icol < 2 Or Icol > 7 Then
NewXML.WriteStartElement(objDataSet.Tables("DataAnalyseVIEW").Columns(Icol).ColumnName().ToString)
NewXML.WriteString(objDataSet.Tables("DataAnalyseVIEW").Rows(Irow)(Icol).ToString())
NewXML.WriteEndElement()
End If
Next
NewXML.WriteEndElement()
Next
NewXML.WriteEndElement()
NewXML.WriteEndElement()
NewXML.Flush()
NewXML.Close()
Catch ex As Exception
Debug.Print("Create - " & ex.Message)
Return False
Finally
If NewXML IsNot Nothing Then
NewXML.Close()
NewXML = Nothing
End If
End Try
Return True
End Function
Private Function SaveXMLFile(ByVal aSection As String, ByVal aKey As String, ByVal aValue As String) As Boolean
Dim Paths() As String
Dim n As Integer
Dim Node, Node2 As XmlNode
Dim Ele As XmlElement
While Strings.Left(aSection, 1) = "/ "
aSection = Strings.Mid(aSection, 2)
End While
If aSection = " " Then
xmlDoc.DocumentElement.RemoveAll()
Else
Paths = Strings.Split(aSection, "/ ")
Try
Node = xmlDoc.DocumentElement.SelectSingleNode(Paths(n))
If Node Is Nothing Then
Ele = xmlDoc.CreateElement(Paths(n))
Node = xmlDoc.DocumentElement.AppendChild(Ele)
End If
For n = 1 To Paths.Length - 1
If Paths(n) = " " Then Continue For
Node2 = Node.SelectSingleNode(Paths(n))
If Node2 Is Nothing Then
Ele = xmlDoc.CreateElement(Paths(n))
Node2 = Node.AppendChild(Ele)
End If
Node = Node2
Next
If aKey = " " Then
Node.RemoveAll()
Else
Ele = Node.Item(aKey)
If Ele Is Nothing Then
Ele = xmlDoc.CreateElement(aKey)
Node.AppendChild(Ele)
End If
If aValue = " " Then
Node.RemoveChild(Ele)
Else
Ele.InnerText = aValue
End If
End If
Catch ex As Exception
Debug.Print(ex.Message)
Return False
End Try
End If
xmlDoc.Save(strFileName)
End Function

时间: 2024-10-26 01:47:44

asp.net 操作XML 按指定格式写入XML数据 WriteXml_实用技巧的相关文章

Asp.net XMLHTTP封装类(GET,Post发送和接收数据)_实用技巧

复制代码 代码如下: /**************************************************************** * 函数名称:SendCommand(SendMethod method, ST_Param p) * 功能说明:向远程发送URL和参数,接受返回信息(无乱码); * 参 数:method:xml发送方法,POST/Get 两种 P:参数结构体 public string Url; //远程URL public string Parameter

asp.net使用母版页中使用ajax脚本取数据_实用技巧

方法如下: 1. 页面中拖入ScriptManager.以便于使用Ajax脚本.同时放在其它客户端控件,用于触发NetPost方法.这里不列出客户端控件. 复制代码 代码如下: <asp:ScriptManager ID="smMaster" runat="server" ScriptMode="Auto" EnablePageMethods="true"> </asp:ScriptManager> 2

asp.net中对象失去焦点时自动提交数据 V2_实用技巧

.aspx页只拉一个TextBox控件: 复制代码 代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "

wcf-WCF如何返回指定格式的XML(soap)

问题描述 WCF如何返回指定格式的XML(soap) soap:Body <?xml version="1.0" encoding="UTF-8"?> <result> <code>1</code> </message> <output> <resultset> <row> <aaa100>AAC004</aaa100> <aaa101>

asp.net使用DataSet的ReadXml读取XML文件及Stream流的方法_实用技巧

本文实例讲述了asp.net使用DataSet的ReadXml读取XML文件及Stream流的方法.分享给大家供大家参考,具体如下: string strxml = "<xml><m><a>1</a><b>2</b></m><m><a>11</a><b>22</b></m><m><a>111</a><

AJAX使用post发送数据xml格式接受数据_实用技巧

注意点:  1. 用POST发送数据,在2号线函数(也是ajax发送数据的函数:ajaxCall)必须加上一句:xmlObject.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 接着使用xmlObject.send(data);发送 2.3号线函数要注意:   1.禁用缓存(建议,不必要):header("Cache-Control:no-cache")

ASP.NET导出Excel打开时提示:与文件扩展名指定文件不一致解决方法_实用技巧

"将页面显示的GridView中的数据,导出到Excel表格中"时遇到这样一个错误: C# 导出Excel文件 打开Excel文件格式与扩展名指定格式不一致.具体提示如图: 解决办法:这里采用"修改注册表的方法"解决此问题,这并没从根上解决问题: 1.打开注册表编辑器方法:开始 -> 运行 -> 输入regedit -> 确定 2.找到注册表子项HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Exc

在asp.net(C#)中采用自定义标签和XML、XSL显示数据_实用技巧

标签定义 复制代码 代码如下: public class Encoding { public string Encode(string cSource) { return System.Web.HttpUtility.HtmlEncode(cSource); } } public class EmList : Label { public override bool EnableViewState { get{ return false;} } public string XslFile{get

解析在.net中使用XSLT转换xml文档的示例详解_实用技巧

XSL即可扩展的样式表文件. 可以格式化xml的显示,也可以将xml转换成需要的另一种格式.学习XSL必须熟悉XPath.XSL和XPath一样简单强大,容易学习.1. XSL既然可以格式化xml的显示样式,我们先来看如何在xml中引用xsl文件如下代码示例:<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="url.