Solved

Select XML node in VB.net

Posted on 2006-06-21
4
1,167 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
ID: 16956156
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
ID: 16956159
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
ID: 16959362
Dim pages As XmlNode = doc.SelectSingleNode("//Pages")

What is the // before Pages?

thanks
   tim
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16960605
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 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

22 Experts available now in Live!

Get 1:1 Help Now