Reading xml attributes.
Reading xml attributes.
Reading xml attributes.
Reading xml attributes.
Reading xml attributes.
Reading xml attributes. Reading xml attributes. Reading xml attributes. Reading xml attributes. Reading xml attributes. Reading xml attributes. Reading xml attributes. Reading xml attributes.
Reading xml attributes. Reading xml attributes.
Reading xml attributes.
Go Back  Xtreme Visual Basic Talk > > > Reading xml attributes.


Reply
 
Thread Tools Display Modes
  #1  
Old 08-09-2009, 10:10 AM
hawkvalley1's Avatar
hawkvalley1 hawkvalley1 is offline
Centurion
 
Join Date: May 2008
Location: Denver, CO USA
Posts: 190
Default Reading xml attributes.[Resolved]


Hi all, I am new to xml, I can build and write ok. At this point I am trying to read all the attributes, there are 3, from a single node. This is what I tried from an example I found:
Code:
Dim xl As New XmlDocument
xl.Load(XMLfile)
Dim rn As XmlNode = xl.SelectSingleNode("p1")
Dim atr1 = rn.Attributes.GetNamedItem("one").Value 'error, object not set to an object instance
Dim atr2 = rn.Attributes.GetNamedItem("two").Value
Dim atr3 = rn.Attributes.GetNamedItem("three").Value

xl.Save(XMLfile)
I have used a messagebox to show the xl.OuterXml, I see the file is loaded.
Thanks...

Last edited by hawkvalley1; 08-09-2009 at 10:29 AM.
Reply With Quote
  #2  
Old 08-09-2009, 10:28 AM
hawkvalley1's Avatar
hawkvalley1 hawkvalley1 is offline
Centurion
 
Join Date: May 2008
Location: Denver, CO USA
Posts: 190
Default

Ok I found 2 ways to do this:
Code:
Dim rn As XmlNode = xl.DocumentElement("p1") ' or
Dim rn As XmlNode = xl.GetElementsByTagName("p1").Item(0)
I love answering my own question...
Reply With Quote
  #3  
Old 08-09-2009, 11:02 AM
AtmaWeapon's Avatar
AtmaWeaponReading xml attributes. AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Don't bother with GetNamedItem; the documentation for it suggests it looks for an XmlNode and I'm not really sure what that means in the context of an attribute. Just index the attributes directly, but be aware the attribute may not be present and thus null:

Code:
Dim attribute1Value As String = Nothing
Dim attribute1 As XmlAttribute = rn.Attributes("one")
If attribute1 IsNot Nothing Then
  attribute1Value = attribute1.Value
End If
Also, always use types for your variables.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #4  
Old 08-09-2009, 11:03 PM
hawkvalley1's Avatar
hawkvalley1 hawkvalley1 is offline
Centurion
 
Join Date: May 2008
Location: Denver, CO USA
Posts: 190
Default

Thanks Atma, I have used the selectSingleNode in other contexts for selecting the node - like when I was adding child nodes - this works fine. Just not sure why it did not work here. Everything works fine now that I am using one of my earlier solutions.

P.S. I always type my variables this was the format they used. I updated my code - copy and paste thing.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Reading xml attributes.
Reading xml attributes.
Reading xml attributes. Reading xml attributes.
Reading xml attributes.
Reading xml attributes.
Reading xml attributes. Reading xml attributes. Reading xml attributes. Reading xml attributes. Reading xml attributes. Reading xml attributes. Reading xml attributes.
Reading xml attributes.
Reading xml attributes.
 
Reading xml attributes.
Reading xml attributes.
 
-->