|
-
Oct 12th, 2004, 08:12 AM
#1
Thread Starter
Addicted Member
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.
-
Oct 13th, 2004, 12:28 AM
#2
Thread Starter
Addicted Member
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.
-
Oct 13th, 2004, 12:35 AM
#3
Thread Starter
Addicted Member
Anyone
Last edited by toytoy; Oct 13th, 2004 at 10:10 AM.
-
Oct 13th, 2004, 08:45 AM
#4
Thread Starter
Addicted Member
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.
-
Oct 13th, 2004, 10:49 PM
#5
Thread Starter
Addicted Member
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
-
Oct 15th, 2004, 02:38 AM
#6
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|