tags:

views:

276

answers:

3
+4  Q: 

Reading gml in c#

I have a problem with reading some gml files in c#. My files do not have schema or namespaces and looks like file from this question:

http://stackoverflow.com/questions/1818147/help-parsing-gml-data-using-c-linq-to-xml

only whitout the schema like this:

<gml:Polygon srsName='http://www.opengis.net/gml/srs/epsg.xml#4283'&gt;
 <gml:outerBoundaryIs>
  <gml:LinearRing>
   <gml:coord>
    <gml:X>152.035953</gml:X>
    <gml:Y>-28.2103190007845</gml:Y>
   </gml:coord>
   <gml:coord>
    <gml:X>152.035957</gml:X>
    <gml:Y>-28.2102020007845</gml:Y>
   </gml:coord>
   <gml:coord>
    <gml:X>152.034636</gml:X>
    <gml:Y>-28.2100120007845</gml:Y>
    </gml:coord>
   <gml:coord>
    <gml:X>152.034617</gml:X>
    <gml:Y>-28.2101390007845</gml:Y>
    </gml:coord>
   <gml:coord>
    <gml:X>152.035953</gml:X>
    <gml:Y>-28.2103190007845</gml:Y>
    </gml:coord>
  </gml:LinearRing>
 </gml:outerBoundaryIs>
</gml:Polygon>

When I try to read the document with XDocument.Load method i get an exception saying: 'gml' namespace is not defined.

I have a lot of gml files so I do not want to add the schema and namespaces to all my files. Does anybody know how to read my files?

+1  A: 

You could add your namespace and type programatically.

Load your file into a string using File.ReadAllText(filename), append the neccesary type and namespace information and parse it using XDocument.Parse instead of Load.

Jens
+1  A: 

Unless you declare the namespace associated with the "gml" prefix your text is not valid Xml + Namespaces.

You could implement a pre-process step that did something like (pseudo code):

string text = ReadFromFile();
text = text.replace(" srsName=", " xmlns:gml=");
xmlDocument.LoadXml(text); 
dkackman
+3  A: 

Use an XmlTextReader with a XmlNamespaceManager. See an example on MSDN here: http://msdn.microsoft.com/en-us/library/system.xml.xmlnamespacemanager.xmlnamespacemanager.aspx

munissor