2011-02-09 7 views
0

LINQ를 SQL XML에 사용하는 데 어려움을 겪고 있습니다. 나는 예제를 따르고 좋은 결과를 얻고있다. 그러나 문제가 발생하여 누군가 나를 도울 수 있기를 바랍니다.다른 중첩 요소에서 정보를 얻으려면 어떻게해야합니까?

<root> 
<MyItem Host="00155DFF045C" LastName="HOUSTON" FirstName="WHITNEY" > 
    <TimeStamp ComputerTime="02/07/2011 - 21:41:53.715"> 
     <Group Name="Group1"> 
      <Variable ID="1001" Value="Happy" > 
      </Variable> 
     </Group> 
     <Group Name="Group2"> 
      <Variable ID="2000" Value="Monday" > 
      </Variable> 
      <Variable ID="2001" Value="Tuesday" > 
      </Variable> 
     </Group> 
     <Group Name="Group3"> 
      <Variable ID="3000" Value="January" > 
      </Variable> 
      <Variable ID="3001" Value="February" > 
      </Variable> 
     </Group> 
     <Group Name="Groupe4"> 
      <Variable ID="4001" Value="108.000000" > 
      </Variable> 
      <Variable ID="4002" Value="800" > 
      </Variable> 
     </Group> 
     <Group Name="CustomGroup"> 
      <Variable ID="1001000" Value="1.000000" > 
      </Variable> 
     </Group> 
    </TimeStamp> 
</MyItem> 
<MyItem> 
... 
... 
</MyItem> 
</root> 

내가 제대로 자습서 {Host="00155DFF045C" LastName="HOUSTON" FirstName="WHITNEY} 덕분에 같은 정보를 얻을.

하지만 좀 더 자세한 정보가 필요합니다. ID="4001"이있는 Value의 변수 요소를 검색하려면 어떻게해야합니까? Groupe4에 속합니까?

내 현재 코드는 다음 코드와

XElement xmlTweets = XElement.Parse(e.Result); 
var toto = from tweet in xmlTweets.Descendants("MyItem").Take(10) 
      orderby tweet.Element("TimeStamp").Attribute("ComputerTime").Value descending 
      select new History { 
       DisplayName = tweet.Attribute("PatientFirstName").Value + " " + 
          tweet.Attribute("PatientLastName").Value 
      }; 

, 나는 제대로 DisplayName 가치를 얻을 수 있습니다. 변수 4001, 즉 값이 108.0000 인 값을 얻고 싶습니다.

+0

코드는 지금까지 적어 두었습니까? 그것을하는 방법을 쉽게 알 수 있도록 도와 줄 것입니다. –

+0

안녕하세요, Jeff, 제 초기 코드를 현재 코드로 업데이트합니다. 희망을 당신이 나를 도울 수 :) – terry

+0

당신이 포맷하려는 소스 코드가 있다면, 그것을 붙여 넣기하고 4 개의 공백으로 들여 쓰기. 그것은 당신을 위해 멋지게 표시됩니다. 그냥 나중에 읽을 수 있도록 조정해야합니다. :) –

답변

2

특정 요소에 대한 참조가있는 경우 Element() 또는 Attribute() 방법을 사용하여 중첩 요소 중 하나에서 값을 가져올 수 있습니다. Elements() 또는 Attributes()을 사용하여 이들에 대한 LINQ 쿼리를 수행하는 목록을 얻을 수 있습니다. 우리는 루트 요소에 대한 참조를 가정하면, 다음과 같은 정보를 얻을 수 있습니다 :

XElement root = ...; 
XElement myItem = root.Element("MyItem");    // get the first "MyItem" element 
string Host = myItem.Attribute("Host").Value;   // get value of "Host" attribute 
string LastName = myItem.Attribute("LastName").Value; // get value of "LastName" attribute 
string FirstName = myItem.Attribute("FirstName").Value; // get value of "FirstName" attribute 

// find "Groupe4" 
XElement Groupe4 = (from g in myItem.Element("TimeStamp") 
            .Elements("Group") 
        where g.Attribute("Name").Value == "Groupe4" 
        select g) // only one element should be found 
        .Single(); // assign that element to variable Groupe4 

// Get Value of Variable with ID = 4001 
double Value4001 = (from v in Groupe4.Elements("Variable") // of all Variable elements 
        where (int)v.Attribute("ID") == 4001 // choose elements where ID = 4001 
        select (double)v.Attribute("Value")) // select the Value 
        .Single();        // assign that value to variable Value 4001 

그래서 쿼리에 그 적용을, 당신은 같은 것을 할 수 있습니다

XElement xmlTweets = XElement.Parse(e.Result); 
var id = 4001; // the ID to find 
var toto = from tweet in xmlTweets.Descendants("MyItem") 
            .Take(10) 
      orderby (DateTime)tweet.Element("TimeStamp") 
            .Attribute("ComputerTime") descending 
      select new History { 
       DisplayName = String.Format("{0} {1}", 
        tweet.Attribute("PatientFirstName").Value, 
        tweet.Attribute("PatientLastName").Value), 

       // find the Variable with matching ID and get its Value as a double 
       Value = (from v in tweet.Descendants("Variable") 
         where (int)v.Attribute("ID") == id 
         select (double)v.Attribute("Value")) 
         .Single() 
      }; 
0

을 당신이 한 번 XElement 변수 이름 myItemElement,

 myItemElement 
      .Element("TimeStamp") 
      .Elements("Group") 
      .FirstOrDefault(x => x.Attribute("Name") != null && x.Attribute("Name").Value == "Groupe4") 
      .Elements("Variable") 
      .FirstOrDefault(x => x.Attribute("ID") != null && x.Attribute("ID").Value == "4001") 
      .Attribute("Value").Value; 
+0

안녕하세요 Smartcaveman, 여러분의 솔루션 또한 훌륭합니다! 둘 다 당신에게 많은 thx. – terry

관련 문제