Reputation: 1966
I don't use .Net unless I have to, and for this project I have to. I like the PHP way of doing things but, it seems I am stuck with using a Collection
of Object
's to store/retrieve the data I need. I have my Collection
loaded with my Objects
's and the code to iterate through the collection, my problem is I am unable to retrieve the "key" as it would be called in PHP (I believe in this case it is actually the name of the object). Consider the following example:
Dim xDoc As XPathDocument = New XPathDocument(fName)
Dim xNav As XPathNavigator = xDoc.CreateNavigator()
Dim sender As XPathNodeIterator
sender = xNav.Select("/data/sender/val")
While (sender.MoveNext())
SenderInfo.Add(sender.Current.GetAttribute("n", ""), sender.Current.Value)
End While
For Each item As Object In SenderInfo
Dim value As String = item.ToString()
//need to store the key here
Dim key As String = Nothing
Next
As you can see, I am navigating an XML document, and creating an Object
with some Key/Value pairs. The Object would look something like this if it were in JSON:
{"name":"John Smith","address1":"123 Anywhere St.","city":"This City","state":"FL"}
When I iterate though the Collection I can only get the value of the object, but I need the Key
, in this instance I want "name","address1","city","state" to be stored in a variable for each iteration.
Any ideas? Or am I going about this the wrong way?
Thanks in advance for the help, I am truly stuck on this one!
Upvotes: 2
Views: 2963
Reputation: 415820
It's hard to be sure right now what you want to do with this code, and that can make all the difference in the world for what I would recommend to do instead. But it sounds like you really want a class like this:
<Serializable()>
Public Class Sender
Public Property Name
Public Property Address1
Public Property City
Public Property State
End Class
And then replace your existing code with something like this:
Dim xmlRdr As New Xml.Serialization.XmlSerializer(GetType(Sender))
Using fileStream As New StreamReader(fName)
Dim mySender As Sender = xmlRdr.Deserialize(fileStream)
End Using
You'll probably need a few more attributes in the class to tweak what the deserializer expects the xml to look like. You can read more about serialization here:
http://msdn.microsoft.com/en-us/library/ms950721.aspx
Upvotes: 3
Reputation: 7309
SenderInfo
should be a Dictionary<String,String>
. Here's the documentation and example code, at the bottom, including VB.NET.
Upvotes: 5