XML追加到当前的XML文件(XML appending to current xml file)

2019-10-16 13:28发布

我有一个艰难的时间,试图追加到我创建一个XML文件。 这是目前我必须创建XML文件中的代码:

 Dim myXmlTextWriter As New XmlTextWriter("d:\doc.xml", Nothing)

 With myXmlTextWriter
        .Formatting = Formatting.Indented
        .Indentation = 3
        .IndentChar = " "

        .WriteStartDocument()
        .WriteComment("Data for 3030")
        .WriteStartElement("3030")
        .WriteElementString("jod", "364887")
        .WriteElementString("aag_SN", "782 YvV0007")
        .WriteElementString("te", "9.03")

        .WriteEndElement()
        .Close()
 End With

我每次运行此代码,它只是将覆盖现有的数据。 我怎样才能附加到现有的数据,仍然具有相同的结构? 我能为我的所有数据不循环,因为用户每次填写出来的东西,直到它的第一次保存无法继续前进。

下面是我所期待的一个例子:

 <?xml version="1.0"?>
 <!--Data for 3030-->
 <3030>
    <jod>364887</jod>
    <aag_SN>782 YvV0007</aag_SN>
    <te>9.03</te>
    <jod>364337</jod>
    <aag_SN>782 Y089702</aag_SN>
    <te>5.00</te>
    <jod>32687</jod>
    <aag_SN>782 YFd3407</aag_SN>
    <te>2.43</te>
    <jod>39007</jod>
    <aag_SN>782 Yv75407</aag_SN>
    <te>3.03</te>
 </3030>

所以,我需要每个我开始一个新的插入时间读值,但我无法找到的代码做什么,我目前所需要的。

任何帮助将是巨大的!

大卫

Answer 1:

望着这Daniweb问题的答案之一似乎工作。 他建议为自己创建的FileStream与附加选项,那么当你创建你的XmlTextWriter使用此流。 你必须检查文件,以写XmlHeader只是一个时间存在。

Dim writeStart As Boolean
If Not IO.File.Exists("C:\temp\doc.xml") Then writeStart = True
Dim xmlFile As IO.FileStream = New IO.FileStream("c:\temp\doc.xml", IO.FileMode.Append)
Dim myXmlTextWriter As New XmlTextWriter(xmlFile, System.Text.Encoding.Default)

With myXmlTextWriter
    .Formatting = Formatting.Indented
    .Indentation = 3
    .IndentChar = CChar(" ")
    If writeStart Then .WriteStartDocument()
    .WriteComment("Data for 3030")
    .WriteStartElement("3030")
    .WriteElementString("jod", "364887")
    .WriteElementString("aag_SN", "782 YvV0007")
    .WriteElementString("te", "9.03")
    .WriteFullEndElement()
    .Close()
End With

我做了,因为多根对象的一些变化,XmlDocument的读者还抱怨你的标签开始与多家即“3030”,所以我前面加上一个字母。 我现在用的是XmlTextWriter的创造只有文件:

Dim writeStart As Boolean
If Not IO.File.Exists("C:\temp\doc.xml") Then writeStart = True

Dim xmlFile As IO.FileStream = New IO.FileStream("c:\temp\doc.xml", IO.FileMode.Append)
Dim myXmlTextWriter As New XmlTextWriter(xmlFile, System.Text.Encoding.Default)
If writeStart Then
    With myXmlTextWriter
        .Formatting = Formatting.Indented
        .Indentation = 3
        .IndentChar = CChar(" ")
        .WriteStartDocument()
        .WriteStartElement("root")
        .WriteEndElement()
    End With
End If
myXmlTextWriter.Close()

AddXmlData("c:\temp\doc.xml", "a3030", "364887", "782 YvV0007", "9.03")

添加那么我用这个子添加您的数据:

Private Sub AddXmlData(xmlfile As String, index As String, jod As String, aag_SN As String, te As String)
    Dim myXmlDocument As New XmlDocument
    Dim myNodes, myChildren As XmlNodeList
    Dim node(3) As XmlNode

    myXmlDocument.Load(xmlfile)
    myNodes = myXmlDocument.GetElementsByTagName("root")

    For Each n As XmlNode In myNodes
        If n.Name = "root" Then
            myChildren = n.ChildNodes
            For Each n1 As XmlNode In myChildren
                If n1.Name = index Then
                    node(1) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "jod", "")
                    node(2) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "aag_SN", "")
                    node(3) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "te", "")
                    node(1).InnerText = jod
                    node(2).InnerText = aag_SN
                    node(3).InnerText = te
                    n1.AppendChild(node(1))
                    n1.AppendChild(node(2))
                    n1.AppendChild(node(3))
                    myXmlDocument.Save(xmlfile)
                    Exit Sub
                End If
            Next
            node(0) = myXmlDocument.CreateNode(XmlNodeType.Element, index, "")
            node(1) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "jod", "")
            node(2) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "aag_SN", "")
            node(3) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "te", "")
            node(1).InnerText = jod
            node(2).InnerText = aag_SN
            node(3).InnerText = te
            node(0).AppendChild(node(1))
            node(0).AppendChild(node(2))
            node(0).AppendChild(node(3))
            n.AppendChild(node(0))
            myXmlDocument.Save(xmlfile)
        End If
    Next
End Sub

这是创建一个XmlDocument看起来像这样:

<?xml version="1.0" encoding="Windows-1252"?>
<root>
  <a3030>
    <jod>364887</jod>
    <aag_SN>782 YvV0007</aag_SN>
    <te>9.03</te>
    <jod>364887</jod>
    <aag_SN>782 YvV0007</aag_SN>
    <te>9.03</te>
    <jod>364887</jod>
    <aag_SN>782 YvV0007</aag_SN>
    <te>9.03</te>
    <jod>364887</jod>
    <aag_SN>782 YvV0007</aag_SN>
    <te>9.03</te>
  </a3030>
</root>


Answer 2:

你必须重写整个文件。 它毕竟只是一个文本文件。

如果数据是小,它可能是更容易使用XmlDocument的。



文章来源: XML appending to current xml file