附加到当前 xml 文件的 XML [英] XML appending to current xml file

查看:34
本文介绍了附加到当前 xml 文件的 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试附加到我创建的 XML 文件时遇到了困难.这是目前我必须创建 XML 文件的代码:

I am having a hard time trying to append to an XML file I create. This is currently the code I have to create the XML file:

 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

每次运行此代码时,它只会覆盖现有数据.我如何追加到现有数据并仍然具有相同的结构?我无法循环获取所有数据,因为用户每次都会填写一些内容,并且在先保存之前无法继续.

Every time I run this code, it just overwrites the existing data. How can I append to the existing data and still have the same structure? I can't loop for all my data because the user will fill something out each time and can't move on until it's saved first.

以下是我正在寻找的示例:

Here is an example of what I am looking for:

 <?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>

所以我每次开始新插入时都需要读取值,但我找不到代码来执行我当前需要的操作.

So I need to read the values in each time I start a new insert, but I am unable to find code to do what I currently need.

任何帮助都会很棒!

大卫

推荐答案

看这个 Daniweb 问题 答案之一似乎有效.他的建议是使用 append 选项自己创建 FileStream,然后在创建 XmlTextWriter 时使用此流.您必须检查该文件是否存在才能仅写入一次 XmlHeader.

Looking at this Daniweb question one of the answers appears to work. What he suggests is to create the FileStream yourself with the append option, then use this stream when you create your XmlTextWriter. You will have to check if the file exists in order to write the XmlHeader only one time.

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 创建文件:

I made some changes because of the multiple root object, the xmlDocument reader is also complaining about your Tags starting with a number i.e. "3030" so I prepended an alpha character. I am using the XmlTextWriter for creation of the file only:

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 添加您的数据:

Add I am then using this Sub to add your data:

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:

Which is creating an xmlDocument looking like this:

<?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>

这篇关于附加到当前 xml 文件的 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆