0

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

1 Answer 1

1

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.)

Sign up to request clarification or add additional context in comments.

4 Comments

Note that a good way to avoid null reference exceptions in LINQ to XML is to do an explicit conversion to string - i.e., var FIPS_Block_Code = (string)block.Attribute("FIPS"); - if the attribute is not found it will return null. Not a likely scenario in this case, but it's a good best practice, especially when you can't count on elements/attributes being present all the time.
This works perfectly ! I knew it had to be the namespace but I just couldnt get it right
@William Don't forget to close the thread by marking the answer as accepted if the issue has been solved.
@WilliamDunn - Best way to thank a person for providing his valuable time in solving your issue is by up-voting \accepting the answer. Check how you can do so here - meta.stackexchange.com/questions/5234/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.