Ceci est mon flux.Lire XML en utilisant XmlDocument
<feed xml:lang="">
<title>NEWS.com.au | Top Stories</title>
<link rel="self" href="http://feeds.news.com.au/public/atom/1.0/news_top_stories_48_48.xml"/>
<link rel="alternate" href="http://news.com.au"/>
<id>http://news.com.au</id>
<rights/>
<entry>
<title>F1’s glaring issues exposed</title>
<link href="www.google.com"/>
<author>
<name>STEVE LARKIN</name>
</author>
<link rel="enclosure" type="image/jpeg" length="2373" href="abc.jpg"/>
</entry>
<entry>
.....
</entry>
</feed>
Voici comment je lis le xml.
string downloadfolder = "C:/Temp/Download/abc.xml";
XmlDocument xml = new XmlDocument();
xml.Load(downloadfolder);
XmlNamespaceManager nsmgr = new System.Xml.XmlNamespaceManager(xml.NameTable);
nsmgr.AddNamespace("atom", "http://www.w3.org/2005/Atom");
string xpath_title = "atom:feed/atom:entry/atom:title";
XmlNodeList nodes_title = xml.SelectNodes(xpath_title, nsmgr);
foreach (XmlNode node_title in nodes_title)
{
Console.WriteLine(node_title.InnerText);
}
string xpath_author = "atom:feed/atom:entry/atom:author";
XmlNodeList nodes_author = xml.SelectNodes(xpath_author, nsmgr);
foreach (XmlNode node_author in nodes_author)
{
Console.WriteLine(node_author.InnerText);
}
string xpath_link = "atom:feed/atom:entry/atom:link";
XmlNodeList nodes_link = xml.SelectNodes(xpath_link, nsmgr);
foreach (XmlNode node_link in nodes_link)
{
Console.WriteLine(node_link.Attributes["href"].Value);
}
Je veux lire le titre, lien, auteur à l'intérieur du nœud <entry>
. Je définis xpath puis itérativement les valeurs de chaque nœud. Y at-il un autre moyen de définir une fois xpath et d'itérer toutes les valeurs du nœud <entry>
Que voulez-vous faire exactement avec les données que vous "lisez"? Évidemment, vous n'essayez pas de faire 'Console.WriteLine'. –
J'ai modifié votre titre. S'il vous plaît voir, "[Les questions devraient inclure" tags "dans leurs titres?] (Http://meta.stackexchange.com/questions/19190/)", où le consensus est "non, ils ne devraient pas". –
merci @JohnSaunders je vais stocker les données dans un datatable. – user3754676