Results 1 to 6 of 6

Thread: VB.NET: Writing attribute and Elements to Xml [Resolved]

Threaded View

  1. #4

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230

    Lightbulb

    Currently, it can append.....
    but the type attributes "1" will write to the tag once i re-run the program...
    Is that any way where it can check with the Xml file and continue the numbers instead of starting from 1....

    Currently Xml file:
    Code:
    <Books>
      <Book type="1">
        <Author>ok</Author>
        <Section>1</Section>
      </Book>
      <Book type="2">
        <Author>no</Author>
        <Section>2</Section>
      </Book>
          <Book type="1">    Not I wanted 
      <Author>Peter</Author>
        <Section>7</Section>
      </Book>
    </Books>
    Hope to become:
    Code:
    <Books>
      <Book type="1">
        <Author>ok</Author>
        <Section>1</Section>
      </Book>
      <Book type="2">
        <Author>no</Author>
        <Section>2</Section>
      </Book>
       <Book type="3">  This is what I wanted 
      <Author>Peter</Author>
        <Section>7</Section>
      </Book>
    </Books>
    My Coding:
    Code:
    Dim m As String = "1"
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    
    Dim doc As New Xml.XmlDocument
    'load file
    doc.Load("data.xml")
    'create node
    'get root node named users
    Dim Usersnode As Xml.XmlElement = doc.SelectSingleNode("//Books")
    'add the new node
    Dim newNode As Xml.XmlElement = doc.CreateElement("Book")
    'add attributes
    newNode.SetAttribute("type", m)
    'add children nodes if any
    Dim child As Xml.XmlElement = doc.CreateElement("Author")
    child.InnerText = txtAuthor.Text
    newNode.AppendChild(child)
    child = doc.CreateElement("Section")
    child.InnerText = txtSection.Text
    newNode.AppendChild(child)
    'add new node to users node
    Usersnode.AppendChild(newNode)
    m += 1
          
    'save doc
    doc.Save("data.xml")
    
    End Sub
    Thanks
    Last edited by toytoy; Dec 3rd, 2004 at 08:00 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width