.net操作XML--按指定格式写入XML数据 WriteXml

从SQL下载数据到本地为XML文件

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
XML写入数据可以使用以下步骤: 1. 创建一个 DocumentBuilder 对象来创建 DOM 文档树。 2. 创建一个 Document 对象,用于表示整个 XML 文档。 3. 创建根元素节点,并添加到 Document 对象中。 4. 创建子元素节点,并添加到根元素节点中。 5. 创建元素属性,并将其添加到元素节点中。 6. 创建文本节点,并将其添加到元素节点中。 7. 将 Document 对象写入 XML 文件中。 下面是一个 Java 示例代码: ```java import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Text; public class WriteXML { public static void main(String[] args) throws Exception { // 创建 DocumentBuilder 对象 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); // 创建 Document 对象 Document doc = builder.newDocument(); // 创建根元素节点 Element root = doc.createElement("root"); doc.appendChild(root); // 创建子元素节点 Element child = doc.createElement("child"); root.appendChild(child); // 创建元素属性 child.setAttribute("name", "value"); // 创建文本节点 Text text = doc.createTextNode("Hello, world!"); child.appendChild(text); // 将 Document 对象写入 XML 文件中 File file = new File("output.xml"); javax.xml.transform.TransformerFactory.newInstance() .newTransformer().transform(new javax.xml.transform.dom.DOMSource(doc), new javax.xml.transform.stream.StreamResult(file)); } } ``` 运行该程序后,将在当前目录下生成一个名为 `output.xml` 的 XML 文件,其内容如下: ```xml <?xml version="1.0" encoding="UTF-8" standalone="no"?> <root> <child name="value">Hello, world!</child> </root> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值