?
Solved

xml writer

Posted on 2008-02-12
5
Medium Priority
?
294 Views
Last Modified: 2013-11-26
Yes I am using in vb visual studio 2003 xmlwriter. Is there anyway that i can just add tags instead of rewriting the xml document.  In other words I do not want to erase everything everytime i click add but just add additional elements.

Dim objXMLWriter As XmlTextWriter
        objXMLWriter = New XmlTextWriter(strXMLPath, Nothing)
 
        'Keep Indented to display for the human eye lol
        objXMLWriter.Formatting = Formatting.Indented
        objXMLWriter.Indentation = 3
 
        objXMLWriter.WriteStartDocument()
        objXMLWriter.WriteComment("Created using an XmlTextWriter - " & Now())
 
        objXMLWriter.WriteStartElement("itemlist")
        objXMLWriter.WriteStartElement("item")
 
        'add attributes to this element's opening tag
        objXMLWriter.WriteAttributeString("ID", "")
        objXMLWriter.WriteAttributeString("case.size", "")
        objXMLWriter.WriteAttributeString("pack", "")
        objXMLWriter.WriteAttributeString("pack.size", "")
 
        objXMLWriter.WriteElementString("company", "")
        objXMLWriter.WriteElementString("description", "")
        objXMLWriter.WriteElementString("unit_price", "")
        objXMLWriter.WriteElementString("case_price", "")
        objXMLWriter.WriteElementString("SRP", "")

Open in new window

0
Comment
Question by:Seven price
  • 3
  • 2
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20878092
You can use an XmlDocument to load the document, and then use CreateElement and AppendChild to create and add a child element to a parent element.

Bob
0
 
LVL 9

Author Comment

by:Seven price
ID: 20878159
Appendchild that is the syntex I was looking for never used it before only writer can i modify the code i have above for Appendchild. And example on how that is used. thats it.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 20878327
Example:
  Public Sub AppendNameToXmlDocument(ByVal fileName As String, ByVal name As String)
    Dim document As New XmlDocument()
    document.Load(fileName)
 
    Dim nameElement As XmlElement = document.CreateElement("name")
    nameElement.InnerText = name
    document.DocumentElement.AppendChild(nameElement)
    document.Save(fileName)
  End Sub

Open in new window

0
 
LVL 9

Author Closing Comment

by:Seven price
ID: 31430278
very accurate Thanks
0
 
LVL 9

Author Comment

by:Seven price
ID: 20878403
Appending to an xml file instead of creating new
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question