Linq to XML解析WMS功能文档

时间:2014-03-10 15:37:31

标签: c# xml linq linq-to-xml

我是LINQ的新手,我在解析xml文件时遇到了问题。该文件是 - >

<?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 as 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>

我有一个名为“Layer”的类。对于XML中的每个层,我想将名称,标题和与之相关的后续样式(名称标签)添加到新的Layer对象。

这是我到目前为止所拥有的。

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>();
}

查询

XDocument xdoc = XDocument.Load(localFilename);


var layers = from 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 do I get the list of styles for each layer?,
             };

这与我的其他代码编译良好。当我运行我的应用程序时,我得到一个对象引用未设置为对象错误的实例。我认为这是因为其中一个图层没有名称元素。我不想在一篇文章中提出两个问题,所以我会以某种方式计算出空检查,但是有关从XML中提取信息到我的对象的任何建议吗?提前致谢。对不起,如果我的问题不是那么简单,我是新问题的在线问题。

@Sergey Berezovskiy您的解决方案适用于此特定XML文档。出于某种原因,我与另一个XML没有相同的运气。由于我将要使用不同的服务器,因此我需要确保它能够全面运行。有关为什么我可以从该xml文件中提取图层的任何见解,而不是下面的那个?非常感谢你。

<?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>

1 个答案:

答案 0 :(得分:3)

某些Layer个元素没有Name个孩子。因此p.Element("Name")会返回null。在尝试获取其值NullReferenceException时,您有p.Element("Name").Value

如果您使用强制转换而不是访问Value属性,则很容易修复(如果找不到元素,则转换为字符串将返回null,而不会抛出异常):

var layers = from 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()
             };

注意:因此,您的图层类中包含Styles作为字符串列表,然后我将样式名称用作列表中的值。


查询返回以下两层:

[
  {
    Styles: [ "default", "stretch" ],
    Name: null,
    Title: "Minnesota composite aerial photography",
    SRS: "EPSG:26915"
  },
  {
    Styles: [],
    Name: "mncomp",
    Title: "Minnesota Composite Imagery",
    SRS: "EPSG:26915"
  }
]