Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Updated 30-Sep-12 18:58pm
v2

1 solution

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).

C#
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)

C#
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
 
Share this answer
 
v3
Comments
Karwa_Vivek 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
MT_ 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 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
MT_ 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 1-Oct-12 6:10am    
ok thanks a lot
was missing the xLoad.Save() function.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900