Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading XML...

Status
Not open for further replies.

CharlieIT

MIS
Apr 9, 2003
157
US
In a VB.NET windows application, I am trying to read the entire "value=" from the following line in a web.config:

<add key=&quot;ConnectionString&quot; value=&quot;provider=SQLOLEDB;Data Source=servername;Database=databasename;UID=username;Password=password&quot; />

Here's the code (this code successfully reads through the web.config file, but never returns the value for &quot;value&quot;):

' open the document:
' &quot;filename&quot; is a string which
' is the location of the web.config

Dim reader as New XmlTextReader(filename)

' move to the start of the document
reader.MoveToContent()

' go through the document
Dim test As String
Do While reader.Read

' kind of node
Select Case reader.NodeType

' check for start of element
Case XmlNodeType.Element

' if it is an element start, is it &quot;value&quot;
if reader.Name = &quot;Value&quot; Then
test = reader.Value

MessageBox.Show (test)


Any Ideas why this doesn't work?



 
Isn't the element &quot;add&quot; and the attribute &quot;value&quot;? So shouldn't you be looking for &quot;add&quot; and then seeing if one of the attributes is &quot;value&quot;?

Hope that helps.

:)
 
Thanks--you are right.

I made the change...

if reader.Name = &quot;add&quot; Then
test = reader.Value

...and I see that it is finding &quot;add&quot;, and then returning nothing. So it looks like it is working--but it isn't doing what it is supposed to.

I tried...

if reader.Name = &quot;add key=&quot; Then
test = reader.Value

...but it still only looks for &quot;add&quot;--and it still doesn't solve my problem of trying to read across to &quot;value=&quot; in that line.

So now that I have found how to find &quot;add&quot; within the web.config (above), does anyone know how I can read across to return &quot;value=&quot;?
 
.value will be the innerxml of the add node - in this case nothing. as Thunderbird said you need to look at the attribute instead
 
er... ok and spider beer sounds better because...? ;)

Have we sorted your problem?
 
Sorry Rilez... I didn't mean that... It's just annoying when people don't bother to get your handle right.
 
I didnt mean it personally.

getting back to the question

have you fixed your problem yet? :)
 
Yes--getting back to the problem...

I have figured out how to parse the XML file to find the value of &quot;value&quot; in the web.config (above). I now need to figure out how to write a new value in its place. You guys have steered me in the right direction so far...would you be able to help further?

Here's the code (it looks ugly to me--there must be another way, but this works):

' open the document...
Dim reader As New XmlTextReader(webcfgloc)

' move to the start of the document...
reader.MoveToContent()

' ignore whitespaces
reader.WhitespaceHandling = WhitespaceHandling.None

' start working through the document...
Dim Test As String
Do While reader.Read

' what kind of node is it?
Select Case reader.NodeType

' is it the start of an element?
Case XmlNodeType.Element

' if it's an element start, is it &quot;add&quot;?
If reader.Name = &quot;add&quot; Then
reader.MoveToAttribute(&quot;value&quot;)
Test = reader.Value
MessageBox.Show(Test)
End If

If reader.Name = &quot;value&quot; Then
End
End If
End Select
Loop
 
right the xmlreader can do just that - read. to write to the xml you will need to use either the xmlwriter or xmldocument. xmldocument represents the xml document as a tree so enables you to go both ways. the xmlwriter and xmlreader are forward only - I would probabily use xmldocument in this case due to the small size of the document. you should be able to find plenty of information around for that.

Good luck
 
Here's some code that I use....

Code:
'loads the XML settings file 
    Private Sub _OpenXMLFile()
        Try
            Dim xmlTR As New XmlTextReader(XMLFileName)
            m_xmlDocument = New XmlDocument()
            m_xmlDocument.Load(xmlTR)
            xmlTR.Close()
            xmlTR = Nothing
        Catch e As Exception
            Debug.WriteLine(&quot;Settings Constructor Error: &quot; & e.ToString)
        End Try
    End Sub

    'saves a Key/Value, creating the Section and Keyname if required
    Private Sub _SaveSetting(ByVal SectionName As String, ByVal KeyName As String, ByVal Setting As String)
        Dim xnSection As XmlNode
        Dim xnKey As XmlNode
        Dim xnAttr As XmlAttribute
        'check the document exists, create if not
        If m_xmlDocument.DocumentElement Is Nothing Then
            Try
                m_xmlDocument.LoadXml(&quot;<?xml version=&quot;&quot;1.0&quot;&quot; encoding=&quot;&quot;UTF-8&quot;&quot;?>&quot; & ControlChars.CrLf & _
                    &quot;<ApplicationSettings>&quot; & ControlChars.CrLf & &quot;</ApplicationSettings>&quot;)
            Catch e1 As Exception
                Debug.WriteLine(&quot;_SaveSetting - Error creating Document: &quot; & e1.ToString)
            End Try
        End If
        xnSection = m_xmlDocument.SelectSingleNode(&quot;//Section[@Name='&quot; & SectionName & &quot;']&quot;)
        'check the Section exists, create if not
        If xnSection Is Nothing Then
            Try
                'create the new Section node...
                xnSection = m_xmlDocument.CreateNode(XmlNodeType.Element, &quot;Section&quot;, &quot;&quot;)
                'add the Name attribute
                xnAttr = m_xmlDocument.CreateAttribute(&quot;Name&quot;)
                xnAttr.Value = SectionName
                xnSection.Attributes.SetNamedItem(xnAttr)
                'get the root XML node and add the new node to the document
                Dim xnRoot As XmlNode = m_xmlDocument.DocumentElement
                xnRoot.AppendChild(xnSection)
                xnRoot = Nothing
            Catch e2 As Exception
                Debug.WriteLine(&quot;_SaveSetting - Error creating Section: &quot; & e2.ToString)
            End Try
        End If
        xnKey = xnSection.SelectSingleNode(&quot;descendant::Key[@Name='&quot; & KeyName & &quot;']&quot;)
        'check the Key exists, create if not
        If xnKey Is Nothing Then
            Try
                'create the new Key node...
                xnKey = m_xmlDocument.CreateNode(XmlNodeType.Element, &quot;Key&quot;, &quot;&quot;)
                'add the Name attribute
                xnAttr = m_xmlDocument.CreateAttribute(&quot;Name&quot;)
                xnAttr.Value = KeyName
                xnKey.Attributes.SetNamedItem(xnAttr)
                'add the Value attribute
                xnAttr = m_xmlDocument.CreateAttribute(&quot;Value&quot;)
                xnAttr.Value = Setting
                xnKey.Attributes.SetNamedItem(xnAttr)
                'add the new node to its Section
                xnSection.AppendChild(xnKey)
            Catch e3 As Exception
                Debug.WriteLine(&quot;_SaveSetting - Error creating Key: &quot; & e3.ToString)
            End Try
        Else
            xnKey.Attributes(&quot;Value&quot;).Value = Setting
        End If
        'save changes
        Try
            m_xmlDocument.Save(XMLFileName)
        Catch e4 As Exception
            Debug.WriteLine(&quot;_SaveSetting - Error Saving File: &quot; & e4.ToString)
        End Try
        xnKey = Nothing
        xnSection = Nothing
    End Sub
[\code]

Hope that helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top