Given the following 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. Does the colon have to be escaped? I thought so but removing it, that line still fails on 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
Since no one answered this question, and I finally solved the issue. Here is the answer. I used XPath instead of LINQ.
To create the proper XPath we need to use the contains and starts-with methods within XPath.
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.