c# - Reading multiple parts of XML -


i'm building windows app using xml file lot of nesting in - this.

i need access attribute go down nodes. need keep doing every "route" object available (which may have multiple destinations , trips).

so end trip object have:

routeno = o destination = queenspark eta = 28 

the xml file:

<jproutepositionet xsi:schemalocation="urn:connexionz-co-nz:jp journeyplanner.xsd" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="urn:connexionz-co-nz:jp">     <content maxarrivalscope="60" expires="2015-06-14t20:21:01+12:00"/>     <platform name="manchester st & gloucester st" platformtag="3330">         <route name="orange line" routeno="o">             <destination name="queenspark">                 <trip wheelchairaccess="true" tripno="5497" eta="28"/>                 <trip wheelchairaccess="true" tripno="5498" eta="59"/>             </destination>         </route>     </platform> </jproutepositionet> 

my c# code:

{        xdocument loadeddata = xdocument.load(streamreader);     string stopname = loadeddata.root.element("platform").attribute("name").value.tostring();      var data = query in loadeddata.descendants("route")                select new trip                {                    routeno = (string)query.attribute("routeno").value ?? "test",                    destination = (string)query.descendants("destination").first().attribute("name").value ?? "test",                    eta = (string)query.element("trip").attribute("eta").value ?? "5"                };      list<trip> trip = new list<trip>();     foreach(var in data)     {         trip.add(a);     }      var dispatcher = coreapplication.mainview.corewindow.dispatcher;     try     {         await dispatcher.runasync(coredispatcherpriority.normal, () =>         {              listview.itemssource = data.tolist();         });      }     catch     {      } } 

right now, code runs , doesn't display anything. debugger shows nothing parsed xml. think i've got descendents / elements mixed up, i'd appreciate help!

the core problem -among other problems- causing code display nothing, xml has default namespace, , descendant elements inherit ancestor default namespace implicitly, unless otherwise specified. need use fully-qualified name address element in namespace, f.e using combination of xnamespace + element's local name .

i personally prefer query syntax specific task, try way :

xnamespace d = "urn:connexionz-co-nz:jp"; var data = route in loadeddata.descendants(d+"route")            destination in route.elements(d+"destination")            trip in destination.elements(d+"trip")            let routeno = (string)route.attribute(d+"routeno") ?? "test"            let currentdestination = (string)destination.attribute(d+"name") ?? "test"            select new trip            {                routeno = routeno,                destination = currentdestination,                eta = (string)trip.attribute("eta") ?? "5"            }; 

dotnetfiddle demo


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -