Reputation:
I have a program that inserts a new lesson to a course XML. Here is the XML file:
Course.xml
<Courses>
<Course>
<Name>ABC course</Name>
<Lessons>
<Lesson>
<Lesson_name>Learn ABC!</Lesson_name>
<Lesson_date>XX/XX/XXXX</Lesson_date>
</Lesson>
'WANT TO ADD A LESSON HERE!!!
</Lessons>
</Course>
<Course>
<Name>DEF Course</Name>
<Lesson>
<Lesson_name>Learn DEF!</Lesson_name>
<Lesson_date>XX/XX/XXXX</Lesson_date>
</Lesson>
</Course>
</Courses>
I would want to insert a lesson with all those nodes such as Lesson
, Lesson_date
, Lesson_name
etc. without affecting the existing lessons and other elements. Is there any simple way?
Upvotes: 1
Views: 11016
Reputation: 101700
Something like this should work:
Dim xmlDoc As XmlDocument = New XmlDocument()
xmlDoc.Load("Course.xml")
Dim courseName = "ABC course"
With xmlDoc.SelectSingleNode("/Courses/Course[Name = '" & courseName & "']/Lessons").CreateNavigator().AppendChild()
.WriteStartElement("Lesson")
.WriteElementString("Lesson_name", "Learn something else!")
.WriteElementString("Lesson_date", "YY/YY/YYYY")
.WriteEndElement()
.Close()
End With
If you want to modify the actual file, you could use xmlDoc.Save()
after that.
Upvotes: 3
Reputation: 6079
By using the DataSet.ReadXml Method you can do this. Change the values in Dataset table and use DataSet.WriteXml Method.
Upvotes: 1