0

我的一个项目遇到了一些麻烦。它是使用 VB.net 的 windows mobile 5 的基本财务经理。一切正常,但是,当一个值保存在 xml 中时,当另一个值被保存时,它将被覆盖。我希望它添加这些值而不是完全覆盖它们。

我的代码:

    Private Sub MenuItem3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
    Dim writer As New XmlTextWriter("product.xml", Nothing)
    writer.WriteStartDocument(True)
    writer.Formatting = Formatting.Indented
    writer.Indentation = 2
    writer.WriteStartElement("Table")
    createNode(DateTimePicker.Text, Subject.Text, Category.Text, Amount.Text, PaymentLabel.Text, writer)
    writer.WriteEndElement()
    writer.WriteEndDocument()
    writer.Close()
End Sub
Private Sub createNode(ByVal eDate As String, ByVal eSubject As String, ByVal eCategory As String, ByVal eAmount As String, ByVal ePayment As String, ByVal writer As XmlTextWriter)
    writer.WriteStartElement("Expenses")
    writer.WriteStartElement("Date")
    writer.WriteString(eDate)
    writer.WriteEndElement()
    writer.WriteStartElement("Subject")
    writer.WriteString(eSubject)
    writer.WriteEndElement()
    writer.WriteStartElement("Category")
    writer.WriteString(eCategory)
    writer.WriteEndElement()
    writer.WriteStartElement("Amount")
    writer.WriteString(eAmount)
    writer.WriteEndElement()
    writer.WriteStartElement("Payment")
    writer.WriteString(ePayment)
    writer.WriteEndDocument()
End Sub
4

2 回答 2

0

您必须使用XmlTextReader,XmlTextWriter最重要的是XmlDocument,XmlElement等等。

使用XmlDocument,您将使用Document Object Model操作文档,它允许您删除、插入和附加 XML 节点等。

有关以这种方式读取/写入 XML 的一些指示,请参阅此链接。有关如何操作文档,请参阅有关XmlDocumentSystem.Xml命名空间的官方文​​档。

于 2012-04-14T03:47:19.320 回答
0

好的,这样的事情怎么样?

Private Sub MenuItem3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click

    Dim doc as New XmlDocument()
    doc.LoadXml("product.xml")

    Dim root as XmlNode = doc.DocumentElement
    Dim list as XmlNodeList = root.SelectNodes("Table")
    Dim table as XmlNode = list.Item(0)

    table.AppendChild(createNode(doc, ... , ... ))

    Dim writer As New XmlTextWriter(Console.Out)
    writer.Formatting = Formatting.Indented
    doc.WriteTo(writer)
    writer.Flush()

End Sub

Function createNode(XmlDocument doc, ByVal eDate As String, ByVal eSubject As String, ByVal eCategory As String, ByVal eAmount As String, ByVal ePayment As String, ByVal writer As XmlTextWriter) As XmlNode

    Dim element as XmlElement = doc.CreateElement("Expenses")

    Dim dateElement as XmlElement = doc.CreateElement("Date")
    dateElement.InnerText = eDate
    element.AppendChild(dateElement)

    ... similar code for the other sub-elements ...

    Return element

End Function
于 2012-04-14T12:23:27.680 回答