0

次のような XML から構築された XElement オブジェクトがあります。

<Root>
  <Oppurtunities>
    <Oppurtunity>
      <Title> Account Manager</Title>
      <Company>Company name</Company>
      <Location>Boston</Location>
      <EndDate>2013-04-11</EndDate>
      <c>acce</c>
      <id>MNYN-95ZL8L</id>
      <Description>This is a detailed description...</Description>
    </Oppurtunity>

ここで、特定の Oppurtunity ノードから説明の値を取得する必要があります。つまり、ID が特定の場所から説明を取得する必要があります。私はこのようなことをする必要があります:

//My XElement object is called oppurtunities
oppurtunities = new XElement((XElement)Cache["Oppurtunities"]);

string id = Request.QueryString["id"];

//I want to do something like this but of course this is not working
var description = (from job in oppurtunities
                              .Element("Oppurtunities")
                              .Element("Oppurtunity")
                              .Element("Description")
                              where job.Element("id") == id).SingleOrDefault();
4

1 に答える 1

0

条件の作業.Element("Description")を許可するには、クエリをさらに移動する必要があります。id

//I want to do something like this but of course this is not working
var description = (from job in oppurtunities
                              .Element("Oppurtunities")
                              .Elements("Oppurtunity")
                   where job.Element("id") == id
                   select job.Element("Description")).SingleOrDefault()

Element("id")文字列として比較するには、(string)XElement変換を使用し<id>ます。見つからない場合でも機能します。

where (string)job.Element("id") == id

を使用XElement.Valueすると、その状況でスローNullReferenceExceptionされます。

于 2013-04-09T08:22:17.587 に答える