2015-07-02 28 views
1

例子的數據存儲的子節點:LINQ/XML - 它有一個自定義的類不同的名字

<property> 
    <price>2080000</price> 
    <country>France</country> 
    <currency>euro</currency> 
    <locations> 
    <location_9>Ski</location_9> 
    <location_16>50km or less to airport</location_16> 
    <location_17>0-2KM to amenities</location_17> 
    </locations> 
    <secondaryproptypes> 
    <secondaryproptypes_1>Holiday Home</secondaryproptypes_1> 
    </secondaryproptypes> 
    <features> 
    <features_30>Woodburner(s)</features_30> 
    <features_9>Private parking</features_9> 
    <features_23>Mountain view</features_23> 
    <features_2>Mains Drains</features_2> 
</features> 

的例子物業類別:

public class Property 
    { 
     public decimal price { get; set; } 
     public string country { get; set; } 
     public string currency { get; set; } 

     public List<Location> locations { get; set; } 
    } 

位置的示例類:

public class Location 
    { 
     public string location { get; set; } 
    } 

主代碼:(也嘗試了很多衍生物,但是當我放棄時,它就是這樣)

public void LoadXMLURL() 
    { 
     XDocument document = XDocument.Load("file.xml"); 

     var properties = (from p in document.Root.Elements("property") 
          select new Property 
          { 

           price = Convert.ToDecimal(p.Element("price").Value), 
           country = p.Element("country").Value, 
           currency = p.Element("currency").Value, 
           locations = new List<Location>(from l in p.Descendants("location") 
                  select new Location 
                  { 
                   location = (string)l 
                  }) 
          } 

          ).ToList(); 

我曾嘗試過多種存儲位置數據節點列表的方法。如數組和其他列表。

現在我認爲我的主要問題是,因爲節點是不同的; 「location_9」 「location_16」

我無法指定要查看的節點,與我以前的節點一樣嚴格。

+0

代替''使用類似'<位置ID =「9」>' –

+0

我有超過的XML的格式沒有控制,對不起,我應該指定此。 – Birksy89

回答

0

好的是,你抓取的節點被組合在<locations>元素中,所以你可以使用p.Element("locations").Elements()而不是p.Desendants("location")

下面是一些代碼,在LINQPad工作:

void Main() 
{ 
    var str = @" 
    <root> 
    <property> 
    <price>2080000</price> 
    <country>France</country> 
    <currency>euro</currency> 
    <locations> 
     <location_9>Ski</location_9> 
     <location_16>50km or less to airport</location_16> 
     <location_17>0-2KM to amenities</location_17> 
    </locations> 
    <secondaryproptypes> 
     <secondaryproptypes_1>Holiday Home</secondaryproptypes_1> 
    </secondaryproptypes> 
    <features> 
     <features_30>Woodburner(s)</features_30> 
     <features_9>Private parking</features_9> 
     <features_23>Mountain view</features_23> 
     <features_2>Mains Drains</features_2> 
    </features></property> 
    </root>"; 
    var document = XDocument.Parse(str); 
    var properties = 
     (from p in document.Root.Elements("property") 
        select new Property 
        { 

         price = Convert.ToDecimal(p.Element("price").Value), 
         country = p.Element("country").Value, 
         currency = p.Element("currency").Value, 
         locations = new List<Location>(from l in p.Element("locations").Elements() 
                select new Location 
                { 
                 location = (string)l 
                }) 
        } 

         ).ToList() 
         .Dump(); 
} 

public class Property 
{ 
    public decimal price { get; set; } 
    public string country { get; set; } 
    public string currency { get; set; } 

public List<Location> locations { get; set; } 
} 

public class Location 
{ 
    public string location { get; set; } 
} 
+0

請你可以擴展你的答案,詳細說明如何用我創建的Location類實現它。 您的建議已成爲我歷史上嘗試過的衆多建議之一,我相信我的錯誤很簡單。所以看到一個完整的例子會真的幫助我。 謝謝 – Birksy89

+0

@ Birksy89:我粘貼了一個工作示例。我所做的唯一修改是添加一個根元素並關閉示例文本中的property標籤,使用Parse而不是Load,然後按我在原始答案中描述的方式更改代碼。 – StriplingWarrior

0

一旦一個有父母,通過Elements只是一般要求孩子。下面是一個例子

locations = p.Descendants("locations") 
      .Elements() 
      .Select (elm => new Location() 
           { 
            location = elm.Value 
           }); 
+0

謝謝你,你的回答讓我最親近。 雖然,我不得不將我的Property類更改爲以下內容; 'public IEnumerable locations {get;組; }' 而不是一個列表,因爲我無法弄清楚如何正確投射它 – Birksy89

相關問題