I'm working with XML for the first time and Ive got an XML File for addresses that includes elements for States Cities, Street, and House Numbers Here is an abbreviated version of my XML file, sorry for it being so long still.
<?xml version="1.0" encoding="UTF-8"?> <Locations> <State> Ohio OH <City> Cincinnati <Street> Walnut Street <Number>650</Number> <Number>600</Number> <Number>700</Number> <Number>414</Number> </Street> <Street> Woolper Ave <Number>110</Number> </Street> <Street> Worth Ave <Number>2022</Number> </Street> <Street> Madison Rd <Number>3081</Number> </Street> <Street> Wasson Rd <Number>3376</Number> </Street> <Street> Wooster Pike <Number>6906</Number> <Number>7453</Number> </Street> <Street> Woodcroft Dr <Number>7314</Number> </Street> <Street> Pike St <Number>316</Number> </Street> <Street> Wocher Ave <Number>119</Number> </Street> <Street> Fairfield Ave <Number>3244</Number> </Street> <Street> Glen Este-Withamsville Rd <Number>4450</Number> </Street> <Street> Linwood Ave <Number>3151</Number> <Number>3209</Number> </Street> <Street> Newtown Rd <Number>3811</Number> </Street> <Street> Round Bottom Rd <Number>3700</Number> </Street> <Street> Main St <Number>7849</Number> </Street> <Street> Eastgate Blvd <Number>4501</Number> </Street> </City> <City> New Richmond <Street> front <Number /> </Street> </City> <City> Batavia <Street> <Number /> </Street> </City> <City> Bethel <Street> <Number /> </Street> </City> <City> Amelia <Street> <Number /> </Street> </City> <City> Williamsburg <Street> <Number /> </Street> </City> <City> Dayton <Street> <Number /> </Street> </City> <City> Columbus <Street> <Number /> </Street> </City> <City> Cleveland <Street> <Number /> </Street> </City> <City> Washington Court House <Street> <Number /> </Street> </City> </State> <State> Alabama AL <City> <Street> <Number /> </Street> </City> </State> <State> Montana MT <City> <Street> <Number /> </Street> </City> </State> <State> Alaska AK <City> <Street> <Number /> </Street> </City> </State> <State> Nebraska NE <City> <Street> <Number /> </Street> </City> </State> <State> Arizona AZ <City> <Street> <Number /> </Street> </City> </State> </Locations> Anyways I'm trying to add the cities that are inside the Ohio OH element.
I've tried using System.Xml, System.Xml.Linq, and System.Xml.XPath;
Xpath shows the most promise but I'm having trouble figuring out if I can return elements belonging to a specific element. Such as all the City elements inside the State Element that contains "Ohio OH". It's entirely possible that I need to have attributes attached to these elements.
My plan is to load the States in to a listbox, then depending on the state selected show the cities that are down tree in another listbox, and so on down the tree.
The code I have now for adding the states is as followed. This works fine because it's only 1 level deep.
XmlNodeList nodeList; XmlNode root = doc.DocumentElement; nodeList = root.SelectNodes("//Locations/State"); foreach (XmlNode state in nodeList) { Listbox1.Items.Add(state.FirstChild.InnerText); } I think my issues lie with dealing with using FirstChild, NextSibling stuff.
Thanks for any help. Hopefully I explained my issue well enough.
No comments:
Post a Comment