Vinez
Vinez

Reputation: 570

nullReferenceExceptionUnhandled error on existing elements parsing XML using XDocument

Tried a 100 variations of parsing this xml I continually get at this point I though I better make a post before I start breaking things(like my monitor)

System.NullReferenceException was unhandled
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=
  StackTrace:
       at Dashboard.Global.geocoder(Object o) in :line 60
       at System.Threading.TimerQueueTimer.CallCallbackInContext(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.TimerQueueTimer.CallCallback()
       at System.Threading.TimerQueueTimer.Fire()
       at System.Threading.TimerQueue.FireNextTimers()
       at System.Threading.TimerQueue.AppDomainTimerCallback()
  InnerException: 

The XML is very simple from FCC.gov

<Response xmlns="http://data.fcc.gov/api" status="OK" executionTime="91">
  <Block FIPS="181770103002004"/>
  <County FIPS="18177" name="Wayne"/>
  <State FIPS="18" code="IN" name="Indiana"/>
</Response>

My code has morphed quite a bit

var xdoc = XDocument.Load(response.GetResponseStream());
XNamespace ns = xdoc.Root.Attribute("xmlns").ToString();
var results = xdoc.Element(ns + "Response").Element(ns + "Block").Attribute("FIPS"); //null ref

if (xdoc != null)
{
    var FIPS_State_Code = results.Value.Substring(0,1); //null ref
    var FIPS_County_Code = xdoc.Element("response"); //nullref
    var Census_Tract = xdoc.Element("response").Element("Block").Attribute("FIPS").Value; //null ref
    var Census_Block_Group = xdoc.Element("response").Element("Block"); //null ref

Answered by tomolak final product (if your actually pull census blocks):

 var xdoc = XDocument.Load(response.GetResponseStream());
                        XNamespace fcc = "http://data.fcc.gov/api";
                        var results = xdoc.Element(fcc + "Response").Element(fcc + "Block").Attribute("FIPS").Value.ToString();
                        if (xdoc != null)
                        {

                            var FIPS_State_Code = results.Substring(0,2);
                            var FIPS_County_Code = results.Substring(2, 3);
                            var Census_Tract = results.Substring(5, 6);
                            var Census_Block_Group = results.Substring(11, 4);
}

Upvotes: 0

Views: 128

Answers (1)

Tomalak
Tomalak

Reputation: 338208

You are not supposed to pull the namespace URI from the input XML, you are supposed to actually put it into your program.

This works just fine:

XNamespace fcc = "http://data.fcc.gov/api";
var response = xdoc.Element(fcc + "Response");
var block = response.Element(fcc + "Block");
var country = response.Element(fcc + "County");
var state = response.Element(fcc + "State");

var FIPS_Block_Code = block.Attribute("FIPS").Value;
var FIPS_County_Code = country.Attribute("FIPS").Value;
var FIPS_State_Code = state.Attribute("FIPS").Value;

Of course you must also use the namespace everywhere, default namespaces like the one in your input XML are inherited.

This won't work:

xdoc.Element("response").Element("Block"); //null ref error

This will:

xdoc.Element(fcc + "Response").Element(fcc + "Block");

(Also note the capital R, XML is of course case-sensitive.)

Upvotes: 1

Related Questions