Linq を使用して xml ドキュメントを照会するにはどうすればよいですか? 「where」句の下のコードを使用すると、「オブジェクト参照がオブジェクトのインスタンスに設定されていません。」がスローされます。具体的には、ノードがnullだと思います。助けてください!
私の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());