如何使用Linq查询xml文档?当我使用下面的代码时,“where”子句抛出“对象引用未设置为对象的实例。”。具体来说,我认为该节点为空。请帮忙!
我的xml看起来像这样:
<qbo:QboCompanyPreferences xmlns="http://www.intuit.com/sb/cdm/v2" xmlns:qbp="http://www.intuit.com/sb/cdm/qbopayroll/v1" xmlns:qbo="http://www.intuit.com/sb/cdm/qbo">
<qbo:Preference>
<Name>TaxAccountId</Name>
<Value>34</Value>
</qbo:Preference>
<qbo:Preference>
<Name>TaxPercent</Name>
<Value>4.5%</Value>
</qbo:Preference>
</qbo:QboCompanyPreferences>
我正在使用这样的XDocument:
XDocument doc = XDocument.Parse(serviceResponse);
string taxNodeName = "TaxPercent";
XNamespace qbo = "http://www.intuit.com/sb/cdm/qbo";
var Preference = (from node in doc.Descendants(qbo + "Preference")
where node.Element("Name").Value == taxNodeName
select node).FirstOrDefault();
Response.Write(Preference.ToString());
答案 0 :(得分:0)
这可能已经处理但是试试这个
XDocument doc = XDocument.Parse(serviceResponse);
string taxNodeName = "TaxPercent";
XNamespace qbo = "http://www.intuit.com/sb/cdm/qbo";
XNamespace ns = "http://www.intuit.com/sb/cdm/v2";
var Preference = (from node in doc.Descendants(qbo + "Preference")
where node.Element(ns+"Name").Value == taxNodeName
select node).FirstOrDefault();
Response.Write(Preference.ToString());
该节点不为null,但未找到Element,因此在.Value
上调用了null
。