OutlawRecon Posted October 14, 2003 Posted October 14, 2003 (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 October 14, 2003 by OutlawRecon Quote
OutlawRecon Posted October 15, 2003 Author Posted October 15, 2003 Anyone??? Comon please, this is due tommorrow. Quote
*Experts* Nerseus Posted October 15, 2003 *Experts* Posted October 15, 2003 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 Quote "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
OutlawRecon Posted October 15, 2003 Author Posted October 15, 2003 Thanks Nerseus. I guess I'll use XPath. It just seems like XmlTextReader is meant for that type of thing. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.