Solved

Select XML node in VB.net

Posted on 2006-06-21
4
1,169 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 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