nullReferenceException对使用XDocument解析XML的现有元素的未处理错误

时间:2015-11-25 16:52:43

标签: xml linq-to-xml nullreferenceexception

尝试解析这个xml的100种变体我不断得到这一点我虽然在开始破坏之前我最好发帖(比如我的显示器)

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: 

来自FCC.gov的XML非常简单

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

我的代码已经变形了很多

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

由tomolak最终产品回答(如果实际拉人口普查阻止):

 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 个答案:

答案 0 :(得分:1)

您不应该从输入XML中提取名称空间URI,您应该将其实际放入您的程序中。

这很好用:

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;

当然,您还必须在任何地方使用命名空间,继承默认命名空间(如输入XML中的命名空间)。

这不会起作用:

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

这将:

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

(另请注意大写字母R,XML当然区分大小写。)