Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: XML VB.NET
How Can I search a Particular Value From XML file.
Below is the Structure of My XML.
I have a from having TextBox and a Button
I want to read the Value of Section
service updateinterval="0.00:02:00"
means I just Want to Fetch the Value 0.00:02:00
in textbox.
and then want to update it after entering some other values.
 
<configuration>
  <configSections>
    <section name="" type=", " />
    <section name="" type="" />
    <section name="" type="" />
    <section name="" type=", , Version=, Culture=, PublicKeyToken=" />
  </configSections>
  <Service1>
    <applications>
      <application id="" manifestUri="" location="" />
      <applicationexe value=",,,,," />
    </applications>
  </Service1>
  <Service21>
    <applications>
      <application id="" manifestUri="" location="" />
      <applicationexe value=",,,,," />
    </applications>
  </Service21>
  <UpdaterConfiguration defaultDownloader="" applicationId="" basePath="">
    <downloaders>
      <add name="" type="," authenticationScheme="" targetServerType="" />
    </downloaders>
    <service updateInterval="0.00:02:00" />
  </UpdaterConfiguration>
  <securityCryptographyConfiguration>
    <hashProviders>
      <add algorithmType=", , Version=, Culture=, PublicKeyToken=" saltEnabled="" type="" />
    </hashProviders>
  </securityCryptographyConfiguration>
</configuration>
 
 
Please assist
how Can I do this
Posted 30-Sep-12 19:57pm
Edited 30-Sep-12 19:58pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I know only c#, but the that's only syntax ! Use following logic if you NOT using LinQ, you can try following to read value. (If using LinQ, let me know).
 
XmlDcoument xDoc = new XmlDocument();
xDoc.Load("YourXml.XML");
 
XmlNode xNode = xDoc.DocumentElement.SelectSingleNode("UpdaterConfiguration/service");
string value = xNode.Attributes["updateInterval"].Value;
 
Use this value to assign to textBox.
 
To update the value (considering its in separate event)
 
XmlDcoument xDoc = new XmlDocument();
xDoc.Load("YourXml.XML");
 
XmlNode xNode = xDoc.DocumentElement.SelectSingleNode("UpdaterConfiguration/service");
xNode.Attributes["updateInterval"].Value = "NewValue";
 
Hope that helps.
 
Thanks
Milind
  Permalink  
v3
Comments
Karwa_Vivek at 1-Oct-12 2:27am
   
Am trying YOur Code to implement.
But while Loading the XML and to fetch the value
I am Getting as error "Data At the root level is Invalid ,Line 1 Position 1"
I tried using a simple XML file .wheres the Problem
Milind Thakkar at 1-Oct-12 2:34am
   
Try to debug and let me know at which line of code its giving problem.
By the way, If you are using xDoc.LoadXml, you have to pass whole XML as string input. If you are reading from a file Please use xDoc.Load.
I have updated the code above. Sorry for confusion.
Karwa_Vivek at 1-Oct-12 5:51am
   
Ok i managed to get the value ,but while updating its not getting updated below is my code,please assist where am missing something
 
Imports System.Xml
Imports System.Collections.Specialized
Imports System.IO
Imports System.Configuration
Imports System.Diagnostics
Imports System.Configuration.ConfigurationSettings
 
Public Class Form1
Dim xDoc As XmlDocument = New XmlDocument()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call FindValue()
End Sub
Public Sub FindValue()
 
xDoc.Load("SynbatAutoUpdater.exe.config")
Dim xNode As XmlNode = xDoc.DocumentElement.SelectSingleNode("UpdaterConfiguration/service")
Dim value As String = xNode.Attributes("updateInterval").Value
TextBox1.Text = value
End Sub
Public Sub UpdateValue()
 

xDoc.Load("SynbatAutoUpdater.exe.config")
Dim xNode As XmlNode = xDoc.DocumentElement.SelectSingleNode("UpdaterConfiguration/service")
xNode.Attributes("updateInterval").Value = "0.00:04:00"
MsgBox("updated")
End Sub
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
UpdateValue()
End Sub
End Class
Milind Thakkar at 1-Oct-12 6:06am
   
Hey, either you need to keep the xDoc.Load at Form1_Load so you are not re-loading values from hard-disk or you will have to save the XML file at the end of UpdateValue function with xDoc.Save();
-Milind
PS: If this solves your question, do mark the answer as solution.
Karwa_Vivek at 1-Oct-12 6:10am
   
ok thanks a lot
was missing the xLoad.Save() function.
Milind Thakkar at 1-Oct-12 6:12am
   
Welcome.
Happy coding.
-Milind

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 519
2 Maciej Los 315
3 Mathew Soji 195
4 BillWoodruff 180
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,712
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 1 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100