查询 XML 到 Linq 的后代

发布于 2024-08-30 10:39:36 字数 1113 浏览 2 评论 0原文

我有以下 xml 数据:

<portfolio>
  <item>
    <title>Site</title>
    <description>Site.com is a </description>
    <url>http://www.site.com</url>
    <photos>
      <photo url="http://www.site.com/site/thumbnail.png" thumbnail="true" description="Main" />
      <photo url="http://www.site.com/site/1.png" thumbnail="false" description="Main" />
    </photos>
  </item>  
</portfolio>

在 c# 中,我使用以下链接查询:

List<PortfolioItem> list = new List<PortfolioItem>();
XDocument xmlDoc = XDocument.Load(HttpContext.Current.Server.MapPath("~/app_data/portfolio.xml"));

list = (from portfolio in xmlDoc.Descendants("item")
       select new PortfolioItem()
       {
           Title = portfolio.Element("title").Value,
           Description = portfolio.Element("description").Value,
           Url = portfolio.Element("url").Value
       }).ToList();

如何查询照片节点?在 PortfolioItem 类中,我有一个属性:

List;照片 {get;set;}

任何想法将不胜感激!

I have the following xml data:

<portfolio>
  <item>
    <title>Site</title>
    <description>Site.com is a </description>
    <url>http://www.site.com</url>
    <photos>
      <photo url="http://www.site.com/site/thumbnail.png" thumbnail="true" description="Main" />
      <photo url="http://www.site.com/site/1.png" thumbnail="false" description="Main" />
    </photos>
  </item>  
</portfolio>

In c# I am using the following link query:

List<PortfolioItem> list = new List<PortfolioItem>();
XDocument xmlDoc = XDocument.Load(HttpContext.Current.Server.MapPath("~/app_data/portfolio.xml"));

list = (from portfolio in xmlDoc.Descendants("item")
       select new PortfolioItem()
       {
           Title = portfolio.Element("title").Value,
           Description = portfolio.Element("description").Value,
           Url = portfolio.Element("url").Value
       }).ToList();

How do I go about querying the photos node? In the PortfolioItem class I have a property:

List<Photo> Photos {get;set;}

Any ideas would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

关于从前 2024-09-06 10:39:36

我对您的 photo 类做出一些假设,假设为了这个答案,您将在构造函数中初始化 url,并通过其属性初始化另外两个类。

最直接的方法就是考虑在邮件 linq 查询中返回一个 Photos 属性,并使用简单的子查询创建它。

    list = (from portfolio in xmlDoc.Descendants("item")
            select new PortfolioItem()
           {
               Title = portfolio.Element("title").Value,
               Description = portfolio.Element("description").Value,
               Url = portfolio.Element("url").Value,
               Photos = (From P In portfilio.Element("photos").Elements("photo")
                   Select new Photo(P.Attribute("url").Value)
                       {
                           .Thumbnail = bool.Parse(P.Attribute("thumbnail").Value),
                           .Description = P.Attribute("description").Value
                       }).ToList()
           }).ToList();

这是一个了解闭包概念的好机会 在 LINQ 中(如果您还没有这样做的话)。

I'm making some assumptions about your photo class, assuming for the sake of this answer that you would initialize url in the constructor, and the other two by their properties.

The most straightforward way is just to consider Photos one more property to return in your mail linq query, and create it with a simple sub-query.

    list = (from portfolio in xmlDoc.Descendants("item")
            select new PortfolioItem()
           {
               Title = portfolio.Element("title").Value,
               Description = portfolio.Element("description").Value,
               Url = portfolio.Element("url").Value,
               Photos = (From P In portfilio.Element("photos").Elements("photo")
                   Select new Photo(P.Attribute("url").Value)
                       {
                           .Thumbnail = bool.Parse(P.Attribute("thumbnail").Value),
                           .Description = P.Attribute("description").Value
                       }).ToList()
           }).ToList();

A good opportunity to check out the concept of closures in LINQ if you haven't already.

蘑菇王子 2024-09-06 10:39:36

假设您的 Photo 类如下所示:

class Photo
{
    public string Url { get; set; }
    public bool Thumbnail { get; set; }
    public string Description { get; set; }
}

您可以这样做:

list = (from portfolio in xmlDoc.Descendants("item")
        select new PortfolioItem()
        {
            Title = portfolio.Element("title").Value,
            Description = portfolio.Element("description").Value,
            Url = portfolio.Element("url").Value,
            Photos = portfolio
                         .Element("photos")
                         .Elements("photo")
                         .Select(e => new Photo {
                             Description = e.Attribute("description").Value,
                             Url = e.Attribute("url").Value,
                             Thumbnail = bool.Parse(e.Attribute("thumbnail").Value),
                         }).ToList()
        }).ToList();

Assuming that your Photo class looks like this:

class Photo
{
    public string Url { get; set; }
    public bool Thumbnail { get; set; }
    public string Description { get; set; }
}

You can do it like this:

list = (from portfolio in xmlDoc.Descendants("item")
        select new PortfolioItem()
        {
            Title = portfolio.Element("title").Value,
            Description = portfolio.Element("description").Value,
            Url = portfolio.Element("url").Value,
            Photos = portfolio
                         .Element("photos")
                         .Elements("photo")
                         .Select(e => new Photo {
                             Description = e.Attribute("description").Value,
                             Url = e.Attribute("url").Value,
                             Thumbnail = bool.Parse(e.Attribute("thumbnail").Value),
                         }).ToList()
        }).ToList();
瑶笙 2024-09-06 10:39:36

不像其他类那么快,但又非常相似:

如果您的 PortfolioItem 和 Photo 类如下所示:

    public class PortfolioItem
    {
        public string Title { get; set; }
        public string Description { get; set; }
        public string Url { get; set; }
        public List<Photo> Photos { get; set; }
    }

    public class Photo
    {
        public string url { get; set; }
        public string thumbnail { get; set; }
        public string description { get; set; }
    }

然后查询照片节点并填充照片列表,如下所示:

    var list = (from portfolio in xmlDoc.Descendants("item")
     select new PortfolioItem()
           {
                    Title = portfolio.Element("title").Value,
                    Description = portfolio.Element("description").Value,
                    Url = portfolio.Element("url").Value,
                    Photos = portfolio.Elements("photos")
                             .Elements("photo")
                             .Select(p => new Photo {
                              url = p.Attribute("url").Value,
                              thumbnail = p.Attribute("thumbnail").Value,
                              description = p.Attribute("description").Value
                              }).ToList()
                     }).ToList();

Not as fast as the others but again something very similar:

If your PortfolioItem and Photo classes look like this:

    public class PortfolioItem
    {
        public string Title { get; set; }
        public string Description { get; set; }
        public string Url { get; set; }
        public List<Photo> Photos { get; set; }
    }

    public class Photo
    {
        public string url { get; set; }
        public string thumbnail { get; set; }
        public string description { get; set; }
    }

Then query the photos node and populate the Photos List like so:

    var list = (from portfolio in xmlDoc.Descendants("item")
     select new PortfolioItem()
           {
                    Title = portfolio.Element("title").Value,
                    Description = portfolio.Element("description").Value,
                    Url = portfolio.Element("url").Value,
                    Photos = portfolio.Elements("photos")
                             .Elements("photo")
                             .Select(p => new Photo {
                              url = p.Attribute("url").Value,
                              thumbnail = p.Attribute("thumbnail").Value,
                              description = p.Attribute("description").Value
                              }).ToList()
                     }).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文