简体   繁体   English

即使存在属性属性,也无法在 xml 中找到元素

[英]Not finding elements in xml even when attribute property is present

Given the following XML鉴于以下 XML

 <?xml version="1.0" encoding="utf-8" ?>
 <headtags>
  <tags page-path-from-root="Models/ModelDashboard">
    <tag type="title" value=""></tag>
    <tag type="meta" name="keyword" content=""></tag>
    <tag type="meta" name="description" content=""></tag>
    <tag type="meta" name="author" content=""></tag>
    <tag type="link" rel="canonical" href="" />
    <tag type="meta" property="og:locale" content="en_US" />
     <tag type="meta" property="og:type" content="website" />
    <tag type="meta" property="og:title" content="Model Dashboard" />
    <tag type="meta" property="og:description" content="Dashboard of values for a {model}" />
    <tag type="meta" property="og:site_name" content="Car Collector Data" />
    <tag type="meta" property="article:publisher" content="Group LLC" />
    <tag type="meta" property="article:author" content="me" />       
 </tags>
 <tags page-path-from-root="Identity/Account/Manage/SiteStatus">
   <tag type="title" value=""></tag>
   <tag type="meta" name="keyword" content=""></tag>
   <tag type="meta" name="description" content=""></tag>
   <tag type="meta" name="author" content=""></tag>
   <tag type="link" rel="canonical" href="" />
   <tag type="meta" property="og:locale" content="en_US" />
   <tag type="meta" property="og:type" content="" />
   <tag type="meta" property="og:title" content="" />
   <tag type="meta" property="og:description" content="" />
   <tag type="meta" property="og:url" content="" />
   <tag type="meta" property="og:site_name" content="" />
   <tag type="meta" property="article:publisher" content="" />
   <tag type="meta" property="article:author" content="" />   
 </tags>
</headtags>

The following method works all the way until the last line where it always errors on finding the elements where the property value contains "og:", which there are clearly six.以下方法一直有效,直到最后一行,在查找属性值包含“og:”的元素时总是出错,显然有六个。 Does the colon have to be escaped?结肠是否必须逃脱? I thought so but removing it, that line still fails on Contains("og")我是这么认为的,但删除它,该行仍然在 Contains("og") 上失败

public List<XElement> GetOpenGraphTagsForPage(string page)
{
   var doc = XDocument.Load(_metaTagXmlFile);
       
  var metaTags =  doc.Descendants("headtags")
                    .Elements("tags")
                  //page value is modeldashboard
                   .Where(u => u.Attribute("page-path-from-root").Value.ToLower().Contains(page))
                  .Elements("tag") //[@type='meta'] didn't work in Xname to eliminate next line...why?
                 .Where(u => u.Attribute("type")?.Value == "meta")
                 .Where(u => u.Attribute("property").Value.Contains("og:"))
                .ToList();

        return metaTags;
    }

Additionally, why doesn't this (the error says "[" character isn't allowed)另外,为什么不这样做(错误说“[”字符是不允许的)

.Elements("tag[@type='meta']") 

produce the same result as this产生与此相同的结果

 .Elements("tag") //[@type='meta'] didn't work in Xname to eliminate next line...why?
 .Where(u => u.Attribute("type")?.Value == "meta")

the Xpath in the first one is asking for the same thing第一个中的 Xpath 要求同样的事情

Since no one answered this question, and I finally solved the issue.由于没有人回答这个问题,我终于解决了这个问题。 Here is the answer.这是答案。 I used XPath instead of LINQ.我使用 XPath 而不是 LINQ。

To create the proper XPath we need to use the contains and starts-with methods within XPath.要创建正确的 XPath,我们需要使用 XPath 中的 contains 和 starts-with 方法。

 public IEnumerable<XElement> GetOpenGraphTagsForPage(string page)
 {
     var doc = XDocument.Load(_metaTagXmlFile);
     var ogTags = doc.XPathSelectElements($"//headtags//tags[contains(@page-path-from-root, '{page}')]//tag[@type='meta'][starts-with(@property, 'og')]");
       
     return ogTags;
 }

I would still like to know the equivalent LINQ, so please post another answer if you have a solution.我仍然想知道等效的 LINQ,所以如果您有解决方案,请发布另一个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM