Sono nuovo nell'analisi XML/HTML. Non so nemmeno le parole giuste per fare una ricerca corretta per i duplicati.Leggere il valore dal nodo HTML
ho questo file HTML che assomiglia a questo:
<body id="s1" style="s1">
<div xml:lang="uk">
<p begin="00:00:00" end="00:00:29">
<span fontFamily="SchoolHouse Cursive B" fontSize="18">I'm great!</span>
</p>
Ora ho bisogno 00:00:00
, 00:00:29
e I'm great!
da esso. Ho potuto leggere in questo modo:
XmlTextReader reader = new XmlTextReader(file);
while (reader.Read())
{
if (reader.NodeType != XmlNodeType.Element)
continue;
if (reader.LocalName != "p")
continue;
var a = reader.GetAttribute(0);
var b = reader.GetAttribute(1);
if (reader.LocalName == "span")
{
XmlDocument doc = new XmlDocument();
doc.Load(reader);
XmlNode elem = doc.DocumentElement.FirstChild;
var c = elem.InnerText;
}
}
ottengo i valori delle variabili a
, b
e c
. Ma c'è stato un leggero cambiamento nel formato HTML. Ora il codice HTML si presenta così:
<body id="s1" style="s1">
<div xml:lang="uk">
<p begin="00:00:00" end="00:00:29">I'm great! </p>
In questo scenario come faccio analizzare fuori 00:00:00
, 00:00:29
e I'm great!
? Ho provato questo:
XmlTextReader reader = new XmlTextReader(file);
while (reader.Read())
{
if (reader.NodeType != XmlNodeType.Element)
continue;
if (reader.LocalName != "p")
continue;
var a = reader.GetAttribute(0);
var b = reader.GetAttribute(1);
XmlDocument doc = new XmlDocument();
doc.Load(reader);
XmlNode elem = doc.DocumentElement.FirstChild;
var c = elem.InnerText;
}
Ma ottengo questo errore: This document already has a 'DocumentElement' node.
alla linea doc.Load(reader)
. Come leggere correttamente e cosa sta causando il problema? Sto usando .NET 2.0
Dai un'occhiata a [html agility pack] (https://htmlagilitypack.codeplex.com/), sembra quello che ti serve per analizzare html. – oleksii
HTML! = XML .... –
@oleksii devo davvero usare lib di terze parti quando ce n'è un sacco sotto System.Xml? Inoltre non sto facendo nulla relativo a html – nawfal