Advertisement

03.03.2008 at 08:49PM PST, ID: 23211723 | Points: 500
[x]
Attachment Details
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
Question Stats
Zone: Microsoft
Question Asked By: ExplorerTechnologies
Question Asked On: 03.03.2008
Participating Experts: 2
Points: 500
Views: 0
Translate:
Loading Advertisement...
03.03.2008 at 09:43PM PST, ID: 21038563

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.03.2008 at 09:50PM PST, ID: 21038600

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.16.2008 at 11:35AM PDT, ID: 21370681

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
03.03.2008 at 09:43PM PST, ID: 21038563
The first xml you posted is invalid.  There is an extra closing tag (</location>) within the xml.  Can you modify the web service to return valid xml or was it copied into your question incorrectly?  You won't be able to determine the structure or nesting with the extra tag included.

Try this...
<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 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>
 
03.03.2008 at 09:50PM PST, ID: 21038600
My apologies l-cm, I must have made a copy-and-paste error:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
<?xml version="1.0" encoding="UTF-8"?>
<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="NT" id="5948.9361">
      <name>Northern Territory</name>
      <location code="ASP" id="5948.9362">
        <name>Alice Springs</name>
      </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 code="SOUTH" id="5948.146">
      <name>South Island</name>
      <location code="CHC" id="5948.156">
        <name>Christchurch</name>
      </location>
      <location code="DUD" id="5948.176">
        <name>Dunedin</name>
      </location>
    </location>
  </location>
  <location code="PACIFIC" id="5948.7447">
    <name>Pacific Islands</name>
    <location code="CK" id="2140.92">
      <name>Cook Islands</name>
    </location>
    <location code="FJ" id="2140.93">
      <name>Fiji</name>
      <location code="CORAL" id="5948.7499">
        <name>Coral &lt;Coast&gt;</name>
      </location>
      <location code="NFIJI" id="5948.7526">
        <name>Fiji's North</name>
      </location>
      <location code="ISLAN" id="5948.7548">
        <name>Island "Resorts"</name>
      </location>
      <location code="NAN" id="5948.7585">
        <name>Nadi</name>
      </location>
      <location code="SUNCO" id="5948.7644">
        <name>Sun-Coast</name>
      </location>
    </location>
    <location code="N0" id="2140.99">
      <name>New Caledonia</name>
    </location>
    <location code="VU" id="2140.108">
      <name>Vanuatu</name>
    </location>
  </location>
</locations>
Open in New Window
 
04.16.2008 at 11:35AM PDT, ID: 21370681
Load that xml into an XmlDatasource and then have your treeview use the XmlDataSource,
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628