Solved

Select XML node in VB.net

Posted on 2006-06-21
4
1,172 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

635 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