vbMarkO
Well-known member
- Joined
- Aug 19, 2005
- Messages
- 157
OK, here is what I am doing and the code I am using..
I have a simple text file that looks like this
These are towns and zipcodes.....
I am putting taking from the textfile and putting them into an xmlFile as seen by the code below
However there is a problem.....
When I get to the towns where there are multiple zipcodes... such as Broken Arrow above...
Its creating an element for Each Broken Arrow zipCode
My xml structure I am wanting should be this
INstead I am getting multiple Broken_Arrow elements....
How can I change my code to make this work?
Seems I need to create the city element but somehow check if it exisits if so then add all the zips associated with that CIty in one element as I did with Broken_Arrow elelment....
vbMarkO
I have a simple text file that looks like this
Code:
74422 Boynton
73011 Bradley
74423 Braggs
74632 Braman
73012 Bray
74010 Bristow
74011 Broken Arrow
74012 Broken Arrow
74013 Broken Arrow
74014 Broken Arrow
74728 Broken Bow
74530 Bromide
73834 Buffalo
74931 Bunch
74633 Burbank
73722 Burlington
73430 Burneyville
73624 Burns Flat
73625 Butler
74831 Byars
73527 Cache
74729 Caddo
74730 Calera
73014 Calumet
74531 Calvin
73835 Camargo
74932 Cameron
74425 Canadian
These are towns and zipcodes.....
I am putting taking from the textfile and putting them into an xmlFile as seen by the code below
However there is a problem.....
When I get to the towns where there are multiple zipcodes... such as Broken Arrow above...
Its creating an element for Each Broken Arrow zipCode
My xml structure I am wanting should be this
Code:
<?xml version="1.0" standalone="yes"?>
<CitiesZipOK>
<Bristow>
<Zip>74010</Zip>
</Bristow>
<Broken_Arrow>
<Zip>74011</Zip>
<Zip>74012</Zip>
<Zip>74013</Zip>
<Zip>74013</Zip>
</Broken_Arrow>
<Broken_Bow>
<Zip>74728</Zip>
</Broken_Bow>
</CitiesZipOK>
INstead I am getting multiple Broken_Arrow elements....
How can I change my code to make this work?
Seems I need to create the city element but somehow check if it exisits if so then add all the zips associated with that CIty in one element as I did with Broken_Arrow elelment....
Code:
Dim reader As New StreamReader(myFile)
Dim xmlDoc As New XmlDocument
Dim cityNode As XmlNode = Nothing
Dim node As XmlNode
Dim line As String
xmlDoc.Load(xmlFile)
line = reader.ReadLine()
Do While (line IsNot Nothing)
Dim myCity As String = line.Substring(6) Returns City or Town
Dim myArr() As String = Split(line, " ")
Dim myZip As String = myArr(0) line.Substring(0, line.IndexOf(" ")) Returns ZipCode
myCity = myCity.Replace(" ", "_")
cityNode = xmlDoc.CreateElement(myCity)
node = xmlDoc.CreateElement("Zip")
node.InnerText = myZip
cityNode.AppendChild(node)
xmlDoc.DocumentElement.AppendChild(cityNode)
line = reader.ReadLine()
Loop
reader.Close()
xmlDoc.Save(xmlFile)
vbMarkO