XML附加到当前xml文件

时间:2012-01-10 02:19:14

标签: xml vb.net xml-parsing

我很难尝试附加到我创建的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>

所以我需要在每次开始新插入时读取值,但是我无法找到代码来执行我当前需要的操作。

任何帮助都会很棒!

大卫

2 个答案:

答案 0 :(得分:3)

查看此Daniweb question其中一个答案似乎有效。他建议使用append选项自己创建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")

添加我然后使用此Sub添加您的数据:

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>

答案 1 :(得分:0)

您必须重写整个文件。毕竟,这只是一个文本文件。

如果数据很小,则使用XmlDocument可能更容易。