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