Solved

Select XML node in VB.net

Posted on 2006-06-21
4
1,164 Views
Last Modified: 2006-11-18
I have

<ImportSession>
  <Batches>
     <Pages>
         <Page filename=C:\uploads\a1.txt />
         <Page filename=C:\uploads\b1.txt />      
     </Pages>
   </Batches>
</ImportSession>

Using VB.Net how can I select the Pages node and add a new page element (<Page filename=C:\uploads\c1.txt />)?

thanks
   tim
0
Comment
Question by:NESupply
  • 3
4 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 125 total points
Comment Utility
Imports System.Xml

  Public Sub AddPage(ByVal xmlFileName As String, ByVal pageFileName As String)

    Dim doc As New XmlDocument
    doc.Load(xmlFileName)

    Dim pages As XmlNode = doc.SelectSingleNode("//Pages")

    Dim newPage As XmlElement = doc.CreateElement("Page")
    Dim attribFileName As XmlAttribute = doc.CreateAttribute("filename")
    attribFileName.Value = pageFileName
    newPage.Attributes.Append(attribFileName)

    pages.AppendChild(newPage)

    doc.Save(xmlFileName)

  End Sub

Sample usage:
   AddPage("c:\temp\test.xml", "c:\test\test.01")

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Note:

You need quotes for the 'filename' attributes that you have shown:

<ImportSession>
  <Batches>
     <Pages>
         <Page filename="C:\uploads\a1.txt" />
         <Page filename="C:\uploads\b1.txt" />      
     </Pages>
   </Batches>
</ImportSession>

Bob
0
 

Author Comment

by:NESupply
Comment Utility
Dim pages As XmlNode = doc.SelectSingleNode("//Pages")

What is the // before Pages?

thanks
   tim
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
If you look at XPath syntax, the '//' means, looks for <Pages> element anywhere within the document.  It is a short-cut expression instead of having to select nodes like this:

  ImportSession/Batches/Pages

Bob
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now