如何将新节点附加到xml中的现有节点

时间:2009-04-20 06:28:05

标签: vb.net

以下是我的xml文件代码

<XMLFile>
 <EMail>
 <From>
  <Address>dddd@acd.com</Address>
 </From>
 <Receipent> <To>eeee@qwe.com</To> </Receipent>
  <Subject>fffffsadasd</Subject> 
  <Body>ggggasdsd</Body> 
  </EMail>
  </XMLFile>

我已经发送了一个按钮。每次我想要将Enail节点和相应的子节点添加到现有的xml文件时点击该按钮。 在发送按钮时,我写了以下代码。

Dim currNode As XmlNode
        Dim doc As New XmlDocument


        doc.LoadXml(("<XMLFile>" + " <EMail></EMail>" + "</XMLFile>"))
        Dim docFrag As XmlDocumentFragment = doc.CreateDocumentFragment()
        docFrag.InnerXml = "<From>" + " <Address>" + txtFrom.Text + " </Address>" + "</From>"
        currNode = doc.DocumentElement.FirstChild
        currNode.InsertAfter(docFrag, currNode.LastChild)

        docFrag.InnerXml = "<Receipent>" + " <To>" + txtTo.Text + " </To>" + "</Receipent>"
        currNode = doc.DocumentElement.FirstChild
        currNode.InsertAfter(docFrag, currNode.LastChild)

        docFrag.InnerXml = "<Subject>" + txtSubject.Text + "</Subject>"
        currNode = doc.DocumentElement.FirstChild
        currNode.InsertAfter(docFrag, currNode.LastChild)

        docFrag.InnerXml = "<Body>" + txtBody.Text + "</Body>"
        currNode = doc.DocumentElement.FirstChild
        currNode.InsertAfter(docFrag, currNode.LastChild)

        doc.Save("C:\xmlmailfile.xml")

我需要在按钮点击中进行哪些修改

1 个答案:

答案 0 :(得分:2)

这样的事情应该这样做。尽管如此,XMLTextWriter可能更适合您的目的:

Private Function GenerateXML(ByVal emails As List(Of Email)) As String

    Dim sb As New System.Text.StringBuilder

    Using sw As New IO.StringWriter(sb), xt As New Xml.XmlTextWriter(sw)

        xt.WriteStartElement("xmlDoc")

        For i As Integer = 0 To emails.Count - 1

            xt.WriteStartElement("email")

            xt.WriteStartElement("From")
            xt.WriteElementString("address", emails(i).From)
            xt.WriteEndElement()

            xt.WriteStartElement("Receipent")
            xt.WriteElementString("to", emails(i).Recipient)
            xt.WriteEndElement()

            xt.WriteElementString("subject", emails(i).Subject)
            xt.WriteElementString("body", emails(i).Body)

            xt.WriteEndElement()

        Next

        xt.WriteEndElement()

    End Using

    Return sb.ToString

End Function

修改

这需要错误处理等,但应该适合你。在某些情况下,它会破坏(例如,如果文件存在但是为空),您需要自己解决。

Module consoleTestApp

    Private _path As String = "c:\output.xml"

    //Just pretend these are text boxes
    Public txtFrom As String
    Public txtRecipient As String
    Public txtSubject As String
    Public txtBody As String

    Sub Main()

        txtFrom = "from1"
        txtRecipient = "rec1"
        txtSubject = "subj1"
        txtBody = "body1"

        AddNewEmail()

        txtFrom = "from2"
        txtRecipient = "rec2"
        txtSubject = "subj2"
        txtBody = "body2"

        AddNewEmail()

    End Sub

    Private Sub AddNewEmail()

        If Not IO.File.Exists(_path) Then

            Using xt As New Xml.XmlTextWriter(_path, System.Text.Encoding.UTF8)

                xt.WriteStartElement("xmlDoc")
                xt.WriteEndElement()

            End Using

        End If

        Dim xD As New Xml.XmlDocument
        xD.Load(_path)

        Dim xN As Xml.XmlNode = xD.CreateNode(Xml.XmlNodeType.Element, String.Empty, "email", String.Empty)
        xN.InnerXml = GenerateXML()

        xD.SelectSingleNode("//xmlDoc").AppendChild(xN)
        xD.Save(_path)


    End Sub

    Private Function GenerateXML() As String

        Dim sb As New System.Text.StringBuilder

        Using sw As New IO.StringWriter(sb), xt As New Xml.XmlTextWriter(sw)

            xt.WriteStartElement("From")
            xt.WriteElementString("address", txtFrom)
            xt.WriteEndElement()

            xt.WriteStartElement("Receipent")
            xt.WriteElementString("to", txtRecipient)
            xt.WriteEndElement()

            xt.WriteElementString("subject", txtSubject)
            xt.WriteElementString("body", txtBody)

        End Using

        Return sb.ToString

    End Function

End Module