LINQ/XML-将具有不同名称的子节点存储在自定义类中

本文关键字:存储 子节点 自定义 XML- LINQ | 更新日期: 2023-09-27 18:00:49

数据示例:

<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();

我确实尝试了许多存储位置数据节点列表的方法。例如数组和其他列表。

现在我认为我的主要问题是,因为节点是不同的;"位置_9"location_16">

我无法指定要查看的节点,就像我对以前的节点一样严格。

LINQ/XML-将具有不同名称的子节点存储在自定义类中

好的是,您正在抓取的节点在<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; }
}

一旦有了父项,一般只需通过Elements请求子项。以下是的示例

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