Wie ein Meta-Tag von XML / HTML mithilfe von Linq extrahieren?
Frage
Ich versuche, ein bisschen von Daten aus einer HTML-Datei zu analysieren, aber meine Linq-Anweisung nicht funktioniert. Hier ist die XML / HTML. Im Folgenden, wie kann ich extrahiere die Zeichenfolge „41,8; 12,23“ aus dem geo.position Meta-Tag? Thx !!
Hier ist meine Linq
String longLat = (String)
from el in xdoc.Descendants()
where
(string)el.Name.LocalName == "meta"
& el.FirstAttribute.Name == "geo.position"
select (String) el.LastAttribute.Value;
Hier ist mein XDocument
<span>
<!--CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="application/xhtml+xml; charset=utf-8" http-equiv="Content-Type" />
<meta content="text/css" http-equiv="Content-Style-Type" />
<meta name="geo.position" content="41.8;12.23" />
<meta name="geo.placename" content="RomeFiumicino, Italy" />
<title>RomeFiumicino, Italy</title>
</head>
<body />
</html>
</span>
Edit: Meine Anfrage als gegebenen Renditen nichts. Die „innere“ Abfrage erscheint eine Liste aller Meta-Elemente, statt nur das ein Element zurückkehrt ich will.
Edit: Die folgende Linq Abfrage funktioniert gegen die gleiche XDocument eine Tabelle mit class name = "data"
retreive var dataTable =
from el in xdoc.Descendants()
where (string)el.Attribute("class") == "data"
select el;
Lösung
Ein span
um Ihren html
Tag?
Sie können dies tun mit XLinq, aber es wäre nur wohlgeformte XML unterstützen. Sie könnten auf der HTML Agility Pack- statt.
Bearbeiten - Dies funktioniert für mich:
string xml = "...";
var geoPosition = XElement.Parse(xml).Descendants().
Where(e => e.Name.LocalName == "meta" &&
e.Attribute("name") != null &&
e.Attribute("name").Value == "geo.position").
Select(e => e.Attribute("content").Value).
SingleOrDefault();
Andere Tipps
Ich würde wetten, dass das Problem, das Sie mit bist kommt aus nicht den Namespace korrekt mit einem XmlNamespaceManager
verweist. Es gibt zwei Möglichkeiten, es zu tun:
string xml =
@"<span>
<!--CTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN""
""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt -->
<html xmlns=""http://www.w3.org/1999/xhtml"">
<head>
<meta content=""application/xhtml+xml; charset=utf-8"" http-equiv=""Content-Type"" />
<meta content=""text/css"" http-equiv=""Content-Style-Type"" />
<meta name=""geo.position"" content=""41.8;12.23"" />
<meta name=""geo.placename"" content=""RomeFiumicino, Italy"" />
<title>RomeFiumicino, Italy</title>
</head>
<body />
</html>
</span>";
string ns = "http://www.w3.org/1999/xhtml";
XmlNamespaceManager nsm;
// pre-Linq:
XmlDocument d = new XmlDocument();
d.LoadXml(xml);
nsm = new XmlNamespaceManager(d.NameTable);
nsm.AddNamespace("h", ns);
Console.WriteLine(d.SelectSingleNode(
"/span/h:html/h:head/h:meta[@name='geo.position']/@content", nsm).Value);
// Linq - note that you have to create an XmlReader so that you can
// use its NameTable in creating the XmlNamespaceManager:
XmlReader xr = XmlReader.Create(new StringReader(xml));
XDocument xd = XDocument.Load(xr);
nsm = new XmlNamespaceManager(xr.NameTable);
nsm.AddNamespace("h", ns);
Console.WriteLine(
xd.XPathSelectElement("/span/h:html/h:head/h:meta[@name='geo.position']", nsm)
.Attribute("content").Value);
I agree with Thorarin - use the HTML Agility pack, it's much more robust.
However, I suspect the problem you are having using LinqToXML is because of the namespace. See MSDN here for how to handle them in your queries.
" If you have XML that is in a default namespace, you still must declare an XNamespace variable, and combine it with the local name to make a qualified name to be used in the query.
One of the most common problems when querying XML trees is that if the XML tree has a default namespace, the developer sometimes writes the query as though the XML were not in a namespace."