How do I convert XML to support IHierachical?
Tags:
C#, Linq
I have the following XML returned to me from a web service: <locations> <location code="AU" id="2140.203"> <name>Australia</name> <location code="NSW" id="5948.9020"> <name>New South Wales</name> <location code="SYD" id="5948.9221"> <name>Sydney</name> <longitude>151.10E</longitude> <latitude>33.53S</latitude> </location> </location> <location code="AYQ" id="5948.9385"> <name>Ayers Rock</name> </location> </location> <location code="VICIA" id="5948.10083"> <name>Victoria</name> <location code="MEL" id="5948.10159"> <name>Melbourne</name> </location> </location> </location> <location code="NZ" id="2140.4"> <name>New Zealand</name> <location code="NORTH" id="5948.27"> <name>North Island</name> <location code="AKL" id="5948.28"> <name>Auckland</name> </location> <location code="WLG" id="5948.127"> <name>Wellington</name> </location> </location> </location> </locations> When I try to pass it to a tree view I get thrown the following exception in Visual Studio (2008.NET C#): HierarchicalDataBoundControl only accepts data sources that implement IHierarchicalDataSource or IHierarchicalEnumerable. I've created a modified version of the XML by hand that seems to work: <locations> <location code="AU" name="Australia" id="2140.203"> <location code="NSW" name="New South Wales" id="2140.203"> <location code="SYD" name="Sydney" id="2140.203" /> <location code="NWC" name="Newcastle" id="2140.203" /> <location code="BAT" name="Bathurst" id="2140.203" /> </location> <location code="VIC" name="Victoria" id="2140.203"> <location code="MEL" name="Melbounre" id="2140.203" /> <location code="GEE" name="Geelong" id="2140.203" /> <location code="TLM" name="Tullamarine" id="2140.203" /> </location> </location> </locations> Does anyone have any suggestions as to how I can either make the XML support hierachies, or better still, a LINQ query that will allow me to rebuild the data into a better format myself? I've a query below that comes close to working, except I can't determine structure or nesting. public String GetLocationsAsTree() { String xmlToReturn = ""; String xmlFromWSDL = ""; xmlFromWSDL = GetLocations(); XDocument myData = XDocument.Parse(xmlFromWSDL); var locations = from location in myData.Descendants("location") select new { id = location.Attribute("id"), code = location.Attribute("code"), name = location.Element("name"), }; foreach (var location in locations) { xmlToReturn += "Name is " + location.name.Value + " (" + location.other.Value + ")" + "<br/>"; } return xmlToReturn; }
Start your free trial to view this solution
Zone:
Microsoft
Question Asked By:
ExplorerTechnologies
Question Asked On:
03.03.2008
Participating Experts:
2
Points:
500
Views:
0
Translate:
JavaScript is required to view Related Solutions
20080236-EE-VQP-29 / EE_QW_2_20070628