c# - Linq to XML Parsing WMS Capabilities Document -


i'm new linq , i'm having trouble parsing xml file. file ->

<?xml version='1.0' encoding="utf-8"?> <!doctype wmt_ms_capabilities system "http://schemas.opengis.net/wms/1.1.1/capabilities_1_1_1.dtd" ><wmt_ms_capabilities version="1.1.1">  <service>   <name>ogc:wms</name>   <title>minnesota composite aerial photography</title>   <onlineresource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="http://geoint.lmic.state.mn.us/"/>  </service>  <capability>   <request>    <getcapabilities>     <format>application/vnd.ogc.wms_xml</format>     <dcptype>      <http>       <get>        <onlineresource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="http://geoint.lmic.state.mn.us/cgi-bin/mncomp"/>       </get>      </http>     </dcptype>    </getcapabilities>    <getmap>     <format>image/jpeg</format>     <dcptype>      <http>       <get>        <onlineresource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="http://geoint.lmic.state.mn.us/cgi-bin/mncomp"/>       </get>      </http>     </dcptype>    </getmap>   </request>   <exception>    <format>application/vnd.ogc.se_xml</format>   </exception>   <layer>    <title>minnesota composite aerial photography</title>    <srs>epsg:26915</srs>    <style>     <name>default</name>     <title>normal</title>     <abstract>colors in original imagery</abstract>    </style>    <style>     <name>stretch</name>     <title>contrast stretch</title>     <abstract>two standard deviation contrast stretch</abstract>    </style>    <layer queryable="0" opaque="1">     <name>mncomp</name>     <title>minnesota composite imagery</title>     <srs>epsg:26915</srs>     <latlonboundingbox minx="-102.05" miny="40.46" maxx="-85.90" maxy="51.37"/>     <boundingbox srs="epsg:26915" minx="-131108.45" miny="4505093.25" maxx="994071.55" maxy="5715117.75" resx="28.5" resy="28.5"/>     <metadataurl type="fgdc">      <format>text/html</format>      <onlineresource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.mngeo.state.mn.us/chouse/metadata/naip09.html"></onlineresource>     </metadataurl>    <scalehint min="11.40" max="40334"/>    </layer>    </layer>  </capability> </wmt_ms_capabilities> 

i have class called "layer". each layer in xml, want add name, title, , subsequent styles(name tag) related new layer object.

this have far.

public class layer {     public string name { get; set; }     public string title { get; set; }     public string srs { get; set; }      public list<string> styles = new list<string>(); } 

query

xdocument xdoc = xdocument.load(localfilename);   var layers = p in xdoc.descendants("layer")              select new layer              {                   name = p.element("name").value,                  title = p.element("title").value,                  srs = p.element("srs").value,                  //styles = //how list of styles each layer?,              }; 

this compiles fine other code. when run application, object reference not set instance of object error. think it's because there isn't name element 1 of layers. don't want ask 2 questions in 1 post i'll work out null checking somehow, suggestions on extracting information xml object? in advance. sorry if question not straightforward needs be, i'm new online question asking thing.

@sergey berezovskiy solution works fine specific xml document. reason i'm not having same luck xml. since i'll hitting different servers need make sure works across board. insight why can extract layers xml file, not 1 below? thank much.

<?xml version="1.0" encoding="utf-8"?> <wms_capabilities version="1.3.0"   xmlns="http://www.opengis.net/wms"   xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"   xmlns:esri_wms="http://www.esri.com/wms"   xsi:schemalocation="http://www.opengis.net/wms http://schemas.opengis.net/wms/1.3.0/capabilities_1_3_0.xsd http://www.esri.com/wms http://www.orthos.dhses.ny.gov/arcgis/services/latest/mapserver/wmsserver?version=1.3.0&amp;service=wms&amp;request=getschemaextension">   <service>     <name><![cdata[wms]]></name>     <title><![cdata[latest]]></title>     <abstract>wms</abstract>     <keywordlist><keyword><![cdata[]]></keyword></keywordlist>     <onlineresource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="http://www.orthos.dhses.ny.gov/arcgis/services/latest/mapserver/wmsserver"/>     <contactinformation>       <contactpersonprimary>         <contactperson><![cdata[]]></contactperson>         <contactorganization><![cdata[]]></contactorganization>       </contactpersonprimary>       <contactposition><![cdata[]]></contactposition>       <contactaddress>         <addresstype><![cdata[]]></addresstype>         <address><![cdata[]]></address>         <city><![cdata[]]></city>         <stateorprovince><![cdata[]]></stateorprovince>         <postcode><![cdata[]]></postcode>         <country><![cdata[]]></country>       </contactaddress>       <contactvoicetelephone><![cdata[]]></contactvoicetelephone>       <contactfacsimiletelephone><![cdata[]]></contactfacsimiletelephone>       <contactelectronicmailaddress><![cdata[]]></contactelectronicmailaddress>     </contactinformation>     <fees><![cdata[]]></fees>     <accessconstraints><![cdata[]]></accessconstraints>     <maxwidth>2048</maxwidth>     <maxheight>2048</maxheight>   </service>   <capability>     <request>       <getcapabilities>         <format>application/vnd.ogc.wms_xml</format>         <format>text/xml</format>         <dcptype>           <http><get><onlineresource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="http://www.orthos.dhses.ny.gov/arcgis/services/latest/mapserver/wmsserver"/></get></http>         </dcptype>       </getcapabilities>       <getmap>         <format>image/bmp</format>         <format>image/jpeg</format>         <format>image/tiff</format>         <format>image/png</format>         <format>image/png8</format>         <format>image/png24</format>         <format>image/png32</format>         <format>image/gif</format>         <format>image/svg+xml</format>         <dcptype>           <http><get><onlineresource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="http://www.orthos.dhses.ny.gov/arcgis/services/latest/mapserver/wmsserver"/></get></http>         </dcptype>       </getmap>       <getfeatureinfo>         <format>application/vnd.esri.wms_raw_xml</format>         <format>application/vnd.esri.wms_featureinfo_xml</format>         <format>application/vnd.ogc.wms_xml</format>         <format>text/xml</format>         <format>text/html</format>         <format>text/plain</format>         <dcptype>           <http><get><onlineresource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="http://www.orthos.dhses.ny.gov/arcgis/services/latest/mapserver/wmsserver"/></get></http>         </dcptype>       </getfeatureinfo>       <esri_wms:getstyles>         <format>application/vnd.ogc.sld+xml</format>         <dcptype>           <http><get><onlineresource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="http://www.orthos.dhses.ny.gov/arcgis/services/latest/mapserver/wmsserver"/></get></http>         </dcptype>       </esri_wms:getstyles>     </request>     <exception>       <format>application/vnd.ogc.se_xml</format>       <format>application/vnd.ogc.se_inimage</format>       <format>application/vnd.ogc.se_blank</format>       <format>text/xml</format>       <format>xml</format>     </exception>     <layer>       <title><![cdata[layers]]></title> <crs>crs:84</crs> <crs>epsg:4326</crs> <crs>epsg:3857</crs> <ex_geographicboundingbox><westboundlongitude>-80.57007947624</westboundlongitude><eastboundlongitude>-71.0436841582623</eastboundlongitude><southboundlatitude>39.9768836585495</southboundlatitude><northboundlatitude>45.5666140920784</northboundlatitude></ex_geographicboundingbox> <boundingbox crs="crs:84"  minx="-80.57007947624" miny="39.9768836585495" maxx="-71.0436841582623" maxy="45.5666140920784"/> <boundingbox crs="epsg:4326"  minx="-80.57007947624" miny="39.9768836585495" maxx="-71.0436841582623" maxy="45.5666140920784"/> <boundingbox crs="epsg:3857"  minx="-8969020.22046862" miny="4862583.64415499" maxx="-7908546.74457592" maxy="5711168.75110067"/>       <layer queryable="1">         <name>0</name>         <title><![cdata[2007]]></title>         <abstract><![cdata[2007]]></abstract> <crs>crs:84</crs> <crs>epsg:4326</crs> <crs>epsg:3857</crs> <ex_geographicboundingbox><westboundlongitude>-79.4438365409271</westboundlongitude><eastboundlongitude>-71.4767021272613</eastboundlongitude><southboundlatitude>40.4072997752793</southboundlatitude><northboundlatitude>43.5397759908541</northboundlatitude></ex_geographicboundingbox> <boundingbox crs="crs:84"  minx="-79.4438365409271" miny="40.4072997752793" maxx="-71.4767021272613" maxy="43.5397759908541"/> <boundingbox crs="epsg:4326"  minx="-79.4438365409271" miny="40.4072997752793" maxx="-71.4767021272613" maxy="43.5397759908541"/> <boundingbox crs="epsg:3857"  minx="-8843647.43040006" miny="4925307.70532643" maxx="-7956750.08438922" maxy="5394495.50256152"/>         <style>           <name>default</name>           <title>0</title>           <legendurl width="0" height="0">             <format>image/png</format>             <onlineresource xlink:href="http://www.orthos.dhses.ny.gov/arcgis/services/latest/mapserver/wmsserver?request=getlegendgraphic%26version=1.3.0%26format=image/png%26layer=0" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" />           </legendurl>         </style>       </layer>       <layer queryable="1">         <name>1</name>         <title><![cdata[2008]]></title>         <abstract><![cdata[2008]]></abstract> <crs>crs:84</crs> <crs>epsg:4326</crs> <crs>epsg:3857</crs> <ex_geographicboundingbox><westboundlongitude>-80.137061507241</westboundlongitude><eastboundlongitude>-72.9541406362735</eastboundlongitude><southboundlatitude>40.2418907294155</southboundlatitude><northboundlatitude>45.3235137050738</northboundlatitude></ex_geographicboundingbox> <boundingbox crs="crs:84"  minx="-80.137061507241" miny="40.2418907294155" maxx="-72.9541406362735" maxy="45.3235137050738"/> <boundingbox crs="epsg:4326"  minx="-80.137061507241" miny="40.2418907294155" maxx="-72.9541406362735" maxy="45.3235137050738"/> <boundingbox crs="epsg:3857"  minx="-8920816.88065531" miny="4901155.6944707" maxx="-8121217.78689084" maxy="5672596.70078496"/>         <style>           <name>default</name>           <title>1</title>           <legendurl width="0" height="0">             <format>image/png</format>             <onlineresource xlink:href="http://www.orthos.dhses.ny.gov/arcgis/services/latest/mapserver/wmsserver?request=getlegendgraphic%26version=1.3.0%26format=image/png%26layer=1" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" />           </legendurl>         </style>       </layer>       <layer queryable="1">         <name>2</name>         <title><![cdata[2009]]></title>         <abstract><![cdata[2009]]></abstract> <crs>crs:84</crs> <crs>epsg:4326</crs> <crs>epsg:3857</crs> <ex_geographicboundingbox><westboundlongitude>-78.2508704377383</westboundlongitude><eastboundlongitude>-72.9360410898466</eastboundlongitude><southboundlatitude>40.6557323735188</southboundlatitude><northboundlatitude>45.2365820956616</northboundlatitude></ex_geographicboundingbox> <boundingbox crs="crs:84"  minx="-78.2508704377383" miny="40.6557323735188" maxx="-72.9360410898466" maxy="45.2365820956616"/> <boundingbox crs="epsg:4326"  minx="-78.2508704377383" miny="40.6557323735188" maxx="-72.9360410898466" maxy="45.2365820956616"/> <boundingbox crs="epsg:3857"  minx="-8710847.05125945" miny="4961694.11943618" maxx="-8119202.954599" maxy="5658843.70875853"/>         <style>           <name>default</name>           <title>2</title>           <legendurl width="0" height="0">             <format>image/png</format>             <onlineresource xlink:href="http://www.orthos.dhses.ny.gov/arcgis/services/latest/mapserver/wmsserver?request=getlegendgraphic%26version=1.3.0%26format=image/png%26layer=2" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" />           </legendurl>         </style>       </layer>       <layer queryable="1">         <name>3</name>         <title><![cdata[2010]]></title>         <abstract><![cdata[2010]]></abstract> <crs>crs:84</crs> <crs>epsg:4326</crs> <crs>epsg:3857</crs> <ex_geographicboundingbox><westboundlongitude>-78.8352781843513</westboundlongitude><eastboundlongitude>-71.5135529969056</eastboundlongitude><southboundlatitude>40.3408670985968</southboundlatitude><northboundlatitude>43.5222862097287</northboundlatitude></ex_geographicboundingbox> <boundingbox crs="crs:84"  minx="-78.8352781843513" miny="40.3408670985968" maxx="-71.5135529969056" maxy="43.5222862097287"/> <boundingbox crs="epsg:4326"  minx="-78.8352781843513" miny="40.3408670985968" maxx="-71.5135529969056" maxy="43.5222862097287"/> <boundingbox crs="epsg:3857"  minx="-8775903.02402806" miny="4915600.50131933" maxx="-7960852.30443332" maxy="5391810.05477954"/>         <style>           <name>default</name>           <title>3</title>           <legendurl width="0" height="0">             <format>image/png</format>             <onlineresource xlink:href="http://www.orthos.dhses.ny.gov/arcgis/services/latest/mapserver/wmsserver?request=getlegendgraphic%26version=1.3.0%26format=image/png%26layer=3" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" />           </legendurl>         </style>       </layer>       <layer queryable="1">         <name>4</name>         <title><![cdata[2011]]></title>         <abstract><![cdata[2011]]></abstract> <crs>crs:84</crs> <crs>epsg:4326</crs> <crs>epsg:3857</crs> <ex_geographicboundingbox><westboundlongitude>-79.4439599225417</westboundlongitude><eastboundlongitude>-72.9501226871704</eastboundlongitude><southboundlatitude>41.8707392833946</southboundlatitude><northboundlatitude>44.5521070703263</northboundlatitude></ex_geographicboundingbox> <boundingbox crs="crs:84"  minx="-79.4439599225417" miny="41.8707392833946" maxx="-72.9501226871704" maxy="44.5521070703263"/> <boundingbox crs="epsg:4326"  minx="-79.4439599225417" miny="41.8707392833946" maxx="-72.9501226871704" maxy="44.5521070703263"/> <boundingbox crs="epsg:3857"  minx="-8843661.16517858" miny="5141636.43859296" maxx="-8120770.51084265" maxy="5551283.37379781"/>         <style>           <name>default</name>           <title>4</title>           <legendurl width="0" height="0">             <format>image/png</format>             <onlineresource xlink:href="http://www.orthos.dhses.ny.gov/arcgis/services/latest/mapserver/wmsserver?request=getlegendgraphic%26version=1.3.0%26format=image/png%26layer=4" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" />           </legendurl>         </style>       </layer>       <layer queryable="1">         <name>5</name>         <title><![cdata[2012]]></title>         <abstract><![cdata[2012]]></abstract> <crs>crs:84</crs> <crs>epsg:4326</crs> <crs>epsg:3857</crs> <ex_geographicboundingbox><westboundlongitude>-80.0942239470773</westboundlongitude><eastboundlongitude>-73.3855444518878</eastboundlongitude><southboundlatitude>40.3409427412789</southboundlatitude><northboundlatitude>43.5208041873471</northboundlatitude></ex_geographicboundingbox> <boundingbox crs="crs:84"  minx="-80.0942239470773" miny="40.3409427412789" maxx="-73.3855444518878" maxy="43.5208041873471"/> <boundingbox crs="epsg:4326"  minx="-80.0942239470773" miny="40.3409427412789" maxx="-73.3855444518878" maxy="43.5208041873471"/> <boundingbox crs="epsg:3857"  minx="-8916048.22527106" miny="4915611.54885857" maxx="-8169241.4399713" maxy="5391582.5351876"/>         <style>           <name>default</name>           <title>5</title>           <legendurl width="0" height="0">             <format>image/png</format>             <onlineresource xlink:href="http://www.orthos.dhses.ny.gov/arcgis/services/latest/mapserver/wmsserver?request=getlegendgraphic%26version=1.3.0%26format=image/png%26layer=5" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" />           </legendurl>         </style>       </layer>       <layer queryable="1">         <name>6</name>         <title><![cdata[2013]]></title>         <abstract><![cdata[2013]]></abstract> <crs>crs:84</crs> <crs>epsg:4326</crs> <crs>epsg:3857</crs> <ex_geographicboundingbox><westboundlongitude>-74.7479376518685</westboundlongitude><eastboundlongitude>-71.6430956456583</eastboundlongitude><southboundlatitude>40.4458244329041</southboundlatitude><northboundlatitude>42.5823674302754</northboundlatitude></ex_geographicboundingbox> <boundingbox crs="crs:84"  minx="-74.7479376518685" miny="40.4458244329041" maxx="-71.6430956456583" maxy="42.5823674302754"/> <boundingbox crs="epsg:4326"  minx="-74.7479376518685" miny="40.4458244329041" maxx="-71.6430956456583" maxy="42.5823674302754"/> <boundingbox crs="epsg:3857"  minx="-8320902.35725337" miny="4930941.35317504" maxx="-7975272.92612848" maxy="5248618.41994531"/>         <style>           <name>default</name>           <title>6</title>           <legendurl width="0" height="0">             <format>image/png</format>             <onlineresource xlink:href="http://www.orthos.dhses.ny.gov/arcgis/services/latest/mapserver/wmsserver?request=getlegendgraphic%26version=1.3.0%26format=image/png%26layer=6" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" />           </legendurl>         </style>       </layer>     </layer>   </capability> </wms_capabilities> 

some layer elements not have name child. p.element("name") returns null. , have nullreferenceexception when trying value p.element("name").value.

it's easy fix if use casting instead of accessing value property (casting string return null if element not found, without throwing exception):

var layers = l in xdoc.descendants("layer")              select new layer              {                  name = (string)l.element("name"),                  title = (string)l.element("title"),                  srs = (string)l.element("srs"),                  styles = l.elements("style")                            .select(s => (string)s.element("name"))                            .tolist()              }; 

note: have styles list of strings in layer class, used style name value in list.


query returns following 2 layers:

[   {     styles: [ "default", "stretch" ],     name: null,     title: "minnesota composite aerial photography",     srs: "epsg:26915"   },   {     styles: [],     name: "mncomp",     title: "minnesota composite imagery",     srs: "epsg:26915"   } ] 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -