2012-06-11 40 views
9
XDocument xDocument = XDocument.Load("..."); 
IEnumerable<XElement> elements = xDocument 
    .Element("lfm") 
    .Element("events") 
    .Elements("event"); 

try 
{    
    foreach (XElement elm in elements) 
    { 
     comm.Parameters.AddWithValue("extID", elm.Element("id").Value ?? ""); 
     comm.Parameters.AddWithValue("Title", elm.Element("title").Value ?? ""); 
     comm.Parameters.AddWithValue("HeadlineArtist", 
     elm.Element("artists").Element("headliner").Value ?? ""); 

nhưng tôi muốn giá trị của phần tử "hình ảnh" có thuộc tính "size = large", tôi đã tìm kiếm cả đêm và đây là tôi đã đi:Tìm XElement với tên và giá trị thuộc tính nhất định với LINQ

comm.Parameters.AddWithValue("LargeImage", 
    elm.Descendants("image") 
     .FirstOrDefault(i => (string)i.Attribute("size") == "large").Value); 

mẫu của một phần của câu trả lời XML:

<lfm status="ok"> 
    <events xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" 
      location="Chicago, United States" page="1" perPage="1" 
      totalPages="341" total="341" festivalsonly="0" tag=""> 
     <event xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"> 
      <id>3264699</id> 
      <title>Iron And Wine</title> 
      <artists> 
       <artist>Iron And Wine</artist> 
       <artist>Dr. John</artist> 
       <headliner>Iron And Wine</headliner> 
      </artists> 
      <venue> 
       <id>8915382</id> 
       <name>Ravinia Festival</name> 
       <location> 
        <city>Highland Park</city> 
        <country>United States</country> 
        <street>200 Ravinia Park Rd</street> 
        <postalcode>60035</postalcode> 
        <geo:point> 
         <geo:lat>42.15831</geo:lat> 
         <geo:long>-87.778409</geo:long> 
        </geo:point> 
       </location> 
       <url>http://www.last.fm/venue/8915382+Ravinia+Festival</url> 
       <website>http://www.ravinia.org/</website> 
       <phonenumber>847.266.5100</phonenumber> 
       <image size="small">http://userserve-ak.last.fm/serve/34/63026487.jpg</image> 
       <image size="medium">http://userserve-ak.last.fm/serve/64/63026487.jpg</image> 
       <image size="large">http://userserve-ak.last.fm/serve/126/63026487.jpg</image> 
       <image size="extralarge">http://userserve-ak.last.fm/serve/252/63026487.jpg</image> 
+0

Vì vậy, vấn đề là những gì? Điều đó có vẻ tốt, nhưng nó phụ thuộc vào những gì 'elm' là (bạn không cho bạn thấy làm thế nào có được từ xDocument để elm). – HackedByChinese

+0

Bạn sẽ nhận được 'NullReferenceException' khi không tìm thấy phần tử nào với thuộc tính đó. – MarcinJuraszek

Trả lời

25

Hãy thử

XElement result = elm.Descendants("image") 
    .FirstOrDefault(el => el.Attribute("size") != null && 
         el.Attribute("size").Value == "large"); 
if (result != null) { 
    process result.Value ... 
} 

Bắt đầu với C# 6.0 (VS 2015), bạn có thể viết:

XElement result = elm.Descendants("image") 
    .FirstOrDefault(el => el.Attribute("size")?.Value == "large"); 
if (result != null) { 
    process result.Value ... 
} 

Một lựa chọn không rõ ràng (như @RandRandom chỉ ra) là để đúc các thuộc tính để string:

XElement result = elm.Descendants("image") 
    .FirstOrDefault(el => (string)el.Attribute("size") == "large"); 
if (result != null) { 
    process result.Value ... 
} 

Tác phẩm này, vì vì XAttribute Explicit Conversion (XAttribute to String).

+0

cho somereason điều này đã không được làm việc lúc đầu, nó hoạt động tuyệt vời mặc dù bây giờ! –

+1

Thay vì? .Giá trị hoặc .Value để bắt đầu, bạn có thể viết như sau: (chuỗi) el.Attribute ("size") == "large" (hoạt động trong tất cả các phiên bản) –

11

bạn có thể sử dụng XPathSelectElement extension method

var node = elm.XPathSelectElement("descendant::image[@size='large']"); 
if (node!=null) 
{ 
    var path = node.Value; 
} 
+1

+1. Giải pháp đơn giản. –

+1

Câu trả lời tuyệt vời !. Thử nghiệm và hoạt động cho .NET Core/.NET Standard 1.6! –

Các vấn đề liên quan