using/reading from XmlTextReader

Gambit_NI

Active member
Joined
Jun 16, 2005
Messages
42
Location
Belfast
Programming Experience
3-5
i have the following XML file which contains settings i want to load into my application. but im having trouble reading the correct nodes. how do i get it to move to the appropriate node. ?

<?xml version="1.0" encoding="utf-8" ?>
- <settings>
<server>192.168.1.16</server>

<timeout>15000</timeout>

</settings>



basically i wanna do something like this

With SettingsReader
.Namespaces =
True
.MoveToContent()

.MoveToElement("server") - this is the bit i cant do!
.Read()
str_Settings = .Value

.MoveToElement("timeout") - and again!
.Read()
g_int_TimeOut =
CType(.Value, Integer)

.Close()
End With

any help would be greatly appreciated!
Cheers,
Craig

 
ive got around it for the mo using this method, but there must be a better way as this uses the actual line number in the file itself, meaning it cant be altered

With SettingsReader
.Namespaces =
True
.MoveToContent()

While
.Read()
If .LineNumber = 3 And .NodeType = Xml.XmlNodeType.Text Then
g_str_Settings = .Value
ElseIf .LineNumber = 4 And .NodeType = Xml.XmlNodeType.Text Then
g_int_TimeOut = CType(.Value, Integer)
End If

End While
.Close()
EndWith
 
I load the xml file into a dataset. Then you can use the .find method of the dataset to search for a value i.e. the server address or someting. I don't have code cause im not at home but have a look at it.
 
and i will go another way , i ll load it into XMLDoc object and use XPath to query and get the value i think bit easy and short as well
Dim doc As new XmlDoc();
doc.Load("file")
doc.selectSginleNode("//server").FirstChild.Value

so woow it done in only two lines
 
cheers for the idea's

@gripusa - what library are you using to get the XmlDoc? within System.XML i can only get xml.xmlDocument, which doesnt have SelectSingleNode
 
take a look at this article http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXmlDocumentClassTopic.asp
.NET Framework Class Library
XmlNode.SelectSingleNode Method



PHP:
Imports System
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

	  Dim doc as XmlDocument = new XmlDocument()
	  doc.Load("booksort.xml")

	  'Create an XmlNamespaceManager for resolving namespaces.
	  Dim nsmgr as XmlNamespaceManager = new XmlNamespaceManager(doc.NameTable)
	  nsmgr.AddNamespace("bk", "urn:samples")

	  'Select the book node with the matching attribute value.
	  Dim book as XmlNode 
	  Dim root as XmlElement = doc.DocumentElement
	  book = root.SelectSingleNode("descendant::book[@bk:ISBN='1-861001-57-6']", nsmgr)

	  Console.WriteLine(book.OuterXml)

  end sub
end class

Follow this on left panel in order to see right article > Net Development> Net Framework >Reference > Clas Library > System.XML > XML Node class > Methods



Cheers ;)
 
Back
Top