Object reference not set to an instance of an object

Hi,

I am trying to read a value in a specific tag in a .gml file with C#2.0. The parser gives me this error message “Object reference not set to an instance of an object”.

This is my gml file:

<?xml version="1.0" encoding="ISO-8859-1"?>

<CityModel xmlns="http://www.opengis.net/citygml/1.0" xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:smil20lang="http://www.w3.org/2001/SMIL20/Language"
    xmlns:smil20="http://www.w3.org/2001/SMIL20/"
    xmlns:xAL="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"
    xmlns:gen="http://www.opengis.net/citygml/generics/1.0"
    xmlns:veg="http://www.opengis.net/citygml/vegetation/1.0"
    xmlns:wtr="http://www.opengis.net/citygml/waterbody/1.0"
    xmlns:trans="http://www.opengis.net/citygml/transportation/1.0"
    xmlns:tex="http://www.opengis.net/citygml/texturedsurface/1.0"
    xmlns:app="http://www.opengis.net/citygml/appearance/1.0"
    xmlns:frn="http://www.opengis.net/citygml/cityfurniture/1.0"
    xmlns:grp="http://www.opengis.net/citygml/cityobjectgroup/1.0"
    xmlns:bldg="http://www.opengis.net/citygml/building/1.0"
    xmlns:luse="http://www.opengis.net/citygml/landuse/1.0"
    xmlns:dem="http://www.opengis.net/citygml/relief/1.0" xmlns:gml="http://www.opengis.net/gml"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="         http://www.opengis.net/citygml/relief/1.0
         http://schemas.opengis.net/citygml/relief/1.0/relief.xsd
         http://www.opengis.net/citygml/landuse/1.0
         http://schemas.opengis.net/citygml/landuse/1.0/landUse.xsd
         http://www.opengis.net/citygml/building/1.0
         http://schemas.opengis.net/citygml/building/1.0/building.xsd
         http://www.opengis.net/citygml/cityobjectgroup/1.0
         http://schemas.opengis.net/citygml/cityobjectgroup/1.0/cityObjectGroup.xsd
         http://www.opengis.net/citygml/cityfurniture/1.0
         http://schemas.opengis.net/citygml/cityfurniture/1.0/cityFurniture.xsd
         http://www.opengis.net/citygml/appearance/1.0
         http://schemas.opengis.net/citygml/appearance/1.0/appearance.xsd
         http://www.opengis.net/citygml/texturedsurface/1.0
         http://schemas.opengis.net/citygml/texturedsurface/1.0/texturedSurface.xsd
         http://www.opengis.net/citygml/transportation/1.0
         http://schemas.opengis.net/citygml/transportation/1.0/transportation.xsd
         http://www.opengis.net/citygml/waterbody/1.0
         http://schemas.opengis.net/citygml/waterbody/1.0/waterBody.xsd
         http://www.opengis.net/citygml/vegetation/1.0
         http://schemas.opengis.net/citygml/vegetation/1.0/vegetation.xsd
         http://www.opengis.net/citygml/generics/1.0
         http://schemas.opengis.net/citygml/generics/1.0/generics.xsd">
<gml:description>
    Written on 2009-02-05T06:14:44+01:00 by the CityGML exporter of the WB3
    project http://www.wb3-project.de for CityGML Version 1.0. Usage, modification and storage
    of this data requires the consent of the WB3 project. All rights remain with the WB3 project
    or organizations named therein. This copyright notice is part of the data and may not be
    removed without explicit consent of the WB3 project.
  </gml:description>
<cityObjectMember>
        <grp:CityObjectGroup gml:id="g_18704">
            <gml:name> g_18704</gml:name>
            <externalReference>
                <informationSystem>WB3 Project</informationSystem>
                <externalObject>
                    <uri>http://www.wb3-project.de/wb_g_id.html?db=waldbruecke&amp;id=18704</uri>
                </externalObject>
</cityObjectMember> 
</CityModel>

and this is the C# codes that try to extract value for that gml file:

XmlTextReader reader = new XmlTextReader(Server.MapPath(@"~\\Files\\Dataset\\"+datasetname));
        reader.Read();
        XmlNamespaceManager nsmanager = new XmlNamespaceManager(reader.NameTable);
        nsmanager.AddNamespace("grp", "http://www.opengis.net/citygml/cityobjectgroup/1.0");
        XmlDocument xmlDocument2 = new XmlDocument();
        xmlDocument2.Load(Server.MapPath(@"~\\Files\\Dataset\\sample.gml"));
        XPathNavigator nav2 = xmlDocument2.CreateNavigator();
        string datasetname2 = nav.SelectSingleNode("CityModel/cityObjectMember/grp:CityObjectGroup/externalReference/informationSystem",nsmanager).Value.ToString();

the last line of code returns that error. Please help me if anyone knows what is the problem?

Amin.

Quick guess is the problem is in these two lines

XPathNavigator nav2 = xmlDocument2.CreateNavigator();
string datasetname2 = nav.SelectSingleNode(“CityModel/cityObjectMember/grp:CityObjectGroup/externalReference/informationSystem”,nsmanager).Value.ToString();

Sorry for wrong typing. the “nav” should be changed to “nav2”. but still the error previously mentioned remains. any idea why?

You need to load your namespaces into your navigator or it can’t find them.

You might also want to look at linq to xml as there really isn’t much reason to use this old xpath stuff unless you are constrained to 2.0.

Yes I am using C#2.0. Should I load all namespaces of gml file or the one that is used in my navigator is enough??
P.S: before that I didn’t enter my namespaces into navigator it gave me different error. Something like"prefixes are un-known…"

Is there anybody to help???

I found my problem. Actually I should have add a default namespace!! Now everything works fine! :>
btw thanks to those guys who cared to help me!