Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 15th, 2008, 07:52 AM
Registered User
 
Join Date: Mar 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default VB.net adding data in a selected node

I have a XML file with this structure. (many more users in it)

Code:
<users>
 <user>
    <counter>1000000</couter>
    <voornaam>Maarten Liesbet</voornaam>
    <achternaam>Verheyen</achternaam>
    <geslacht>M</geslacht>
    <nummerplaat>EEC-718</nummerplaat>
    <naam>test</naam>
    <adres>
    </adres>
    <email>
    </email>
    <telefoon>
    </telefoon>
    <sitebed>
    </sitebed>
    <datum>14/05/2008</datum>
    <uur>10:13:30</uur>
    <logoff>0:00:00</logoff>
    <kaartnummer>590258076812</kaartnummer>
  </user>
</users>
I have a form with a textfield. In the texfield a number can be entered. (counter)
and then I want the logoff time adjusted.
So the programme has to navigate to the correct node with the corresponding counternumber(xpath???) and then change the element "logoff" to the time of the moment (logoff = TimeOfDay)

Is this even possible ?
thanx in advance


 
Old May 15th, 2008, 07:57 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Answer: Of course it is.

Assuming you have an XmlDocument containing the above document, then use SelectSingleNode to select the node with the correct counter, and then just update the text value of the logoff node.

/- Sam Judson : Wrox Technical Editor -/
 
Old May 15th, 2008, 07:59 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

You can use System.Xml.XmlDocument to load your XML document and keep it in memory, then you can use XPath to find the logoff element:
Code:
Dim doc as New XmlDocument()
doc.Load("users.xml")


Dim logoff As XmlNode = doc.SelectSingleNode(String.Format("/users/user[counter = {0}]/logoff", textField.Text))
If logoff IsNot Nothing Then
  logoff.InnerText = ' get current time here
Else
  ' output message that there is no user with the counter
End If
--
  Martin Honnen
  Microsoft MVP - XML
 
Old May 15th, 2008, 08:04 AM
Registered User
 
Join Date: Mar 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Wow what a fast reply

I'm going to test it right away. and keep you posted

thx already

 
Old May 15th, 2008, 08:45 AM
Registered User
 
Join Date: Mar 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry but it doest work.
I builded in 2 messageboxes.
First says's if the number does not exist.
  and if I enter a wrong number the messagebox shows.
the second say's (your logged off)
 and if I enter a correct number this messagebox comes up but in my XML file the element logoff stay's 0:00:00

no error message is shown

 
Old May 15th, 2008, 08:50 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well XmlDocument is an in-memory object model.
The code I showed changes the logoff value in the object model.
To persist changes to a file you need to call
Code:
doc.Save("users.xml")

--
  Martin Honnen
  Microsoft MVP - XML
 
Old May 15th, 2008, 08:51 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

What doesn't work? You aren't showing us any code?

Are you saving the XML back to the file?

/- Sam Judson : Wrox Technical Editor -/
 
Old May 15th, 2008, 08:55 AM
Registered User
 
Join Date: Mar 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

 
Quote:
quote:doc.Save("users.xml")
Quote:
I feel pretty stupid right now.
I've saved the file hundreds of times in other classes and here I forget it.

It works perfect.

Thanx very much

 
Old May 16th, 2008, 06:01 AM
Registered User
 
Join Date: Mar 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If there are more users with the same counter number would it be possible to check if logout =/= 0:00:00
or where the datum+uur is the biggest ?

 
Old May 16th, 2008, 06:37 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I suggest you look at the SelectNodes method, rather than the SelectSingleNode. This will return a XmlNodeList which you can loop through and do your checking.

/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
VB.net, adding XML data to an existing XML file saikoboarder XML 11 April 17th, 2008 04:19 PM
All file name in selected folder VB.net Venkatesan VS.NET 2002/2003 0 February 8th, 2007 02:17 AM
Adding New Record to Access Database Using VB.NET TechHelp ASP.NET 1.0 and 1.1 Basics 1 September 13th, 2006 03:19 AM
To remove a XML node in VB.Net jkusmanto XML 4 May 23rd, 2006 09:18 AM
Adding style to text selected by user Jonax HTML Code Clinic 3 June 9th, 2004 03:54 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.