Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Hi, for months I have been trying to get XmlTextReader to work. All the code examples online don't seem to work for me. Could someone please give me an example:

 

xml:

<Settings>

<Site Name="Learn .NET">

<Footer>

<P>A re-engineering of your current world view</P>

<P>© Copyright 2003 RaveBB Inc. All Rights Reserved</P>

</Footer>

</Site>

</Settings>

 

O.k, to get the site name I am using:

Public Shared Function GetSiteName() As String 
       Dim reader As XmlTextReader = New XmlTextReader(HttpContext.Current.Server.MapPath(("settings.xml"))) 
       Dim strSiteName As String 
       While reader.Read 
           reader.MoveToContent() 
           If reader.Name = "Site" Then 
               reader.MoveToAttribute("Name") 
               strSiteName = reader.Value 
           End If 
       End While 
       Return strSiteName 
   End Function 

to get the footer I am using:

 

Public Shared Function GetFooter() As String 
       Dim strFooter As String 
       Dim reader As New XmlTextReader(HttpContext.Current.Server.MapPath(("settings.xml"))) 
       While reader.Read 
           reader.MoveToContent() 
           If reader.Name = "Site" Then 
               strFooter = reader.ReadString 
           End If 
       End While 
       Return strFooter 
   End Function 

 

For both functions, it returns nothing. I have played around with dozens of ways of reading the xml but none if it works. Could someone please help?

 

Edit: BTW, I want to use XmlTextReader for this because it is faster. I might be able to figure it out with XPath or something but I want to get it working in TextReader for once.

 

Thx

Edited by OutlawRecon
  • *Experts*
Posted

If you need it fast, I'd use an XmlDocument. You can use the load method to load a file directly and use XPath to get at your value. You can use something like:

doc.SelectSingleNode("//Site")

or

doc.SelectSingleNode("//Site").Attributes.GetByName("Name')

 

The last line isn't right, I can't remember the syntax for getting attributes from an XmlDocument.

 

I can't help with XmlTextReader as I haven't used it in ages - but you wanted something fast.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...