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

时间:2021-01-14 14:05:44

Following is my xml file code

以下是我的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>

i hve a sent button.On clicking that button each time i want to append Enail node and correponding childnodes to existing xml file. In sent buttonclick i hve written following code.

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

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")

What modification i have to make in button click

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

1 个解决方案

#1


Something like this should do it. An XMLTextWriter might be better for your purposes though:

这样的事情应该做到这一点。尽管如此,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

EDIT:

This need error handling etc, but should work for you. There are some cases where it will break (such as if a file exists but is empty) which you will need to solve yourself.

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

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

#1


Something like this should do it. An XMLTextWriter might be better for your purposes though:

这样的事情应该做到这一点。尽管如此,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

EDIT:

This need error handling etc, but should work for you. There are some cases where it will break (such as if a file exists but is empty) which you will need to solve yourself.

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

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