Results 1 to 6 of 6

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

  1. #1

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

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

    Say i have this Xml file
    Code:
    <Books>
       <Book type="1">
          <Author>Peter</Author> 
           <Section>1</Section> 
      </Book>
    </Books>
    how to create another automatic set of IDs and datas for the Xml tag so that when i type in another set of data...

    The content of the Xml file will become:
    Code:
    <Books>
      <Book type="1">
           <Author>Peter</Author> 
           <Section>1</Section> 
      </Book>
      <Book type="2">
           <Author>John</Author> 
           <Section>2</Section> 
      </Book>
    </Books>
    Thanks
    Last edited by toytoy; Dec 3rd, 2004 at 07:59 AM.

  2. #2

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    Can anyone help me in the coding...

    I want to do something like that..
    It will compare whether is there a set of tags...
    if yes, it will append to it...
    if no, it will create a new one...

    Thanks
    Last edited by toytoy; Oct 13th, 2004 at 10:11 AM.

  3. #3

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    Anyone
    Last edited by toytoy; Oct 13th, 2004 at 10:10 AM.

  4. #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.

  5. #5

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

    how to do a loop in the Xml file to get the total number of types attribute....

    then from there compare the count to the add button and add accordingly....

    Thanks

  6. #6

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    finally solve it... get it from other forums..

    Code:
     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")
    
    'get current book nodes
    Dim BookNodes As Xml.XmlNodeList = doc.SelectNodes("//Books/Book")
    m = BookNodes.Count() + 1
    
    '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)
    
    'save doc
    doc.Save("data.xml")
     End Sub
    Last edited by toytoy; Dec 3rd, 2004 at 08:01 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