Sunday 15 March 2015

c# - LINQ XML file parsing -



c# - LINQ XML file parsing -

i'm trying parse below xml info values of name , total each metricresponse

<metricresponses xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="http://www.w3.org/2001/xmlschema-instance"> <metricresponse> <code>success</code> <data> <displayname>cpu time</displayname> <endtime>2013-04-16t17:10:37.6101155z</endtime> <name>cputime</name> <primaryaggregationtype>total</primaryaggregationtype> <starttime>2013-04-16t17:00:00z</starttime> <timegrain>pt1h</timegrain> <unit>milliseconds</unit> <values> <metricsample> <count>1</count> <maximum i:nil="true" /> <minimum i:nil="true" /> <timecreated>2013-04-16t17:00:00z</timecreated> <total>390</total> </metricsample> </values> </data> <message /> </metricresponse>

i have written next linq query it:

xnamespace ns = "http://schemas.microsoft.com/windowsazure"; var metrics = b in doc.root.descendants(ns + "metricresponse") select new { name=b.element(ns+"data").element(ns+"name"), total=b.element(ns+"data").element(ns+"values").element(ns+"metricsample").element(ns+"total") };

but when nullreferenceexception when looping through metrics. query works fine getting name value , exception occurs total expression.

c# xml linq

No comments:

Post a Comment