[英]Parsing a feed in C#
我在使用C#
解析提要時遇到問題。
我無法讓提要的作者更改代碼,因此我必須處理它。
我試圖將提要直接作為URL傳遞到XmlDocument對象中,或者使用WebClient作為文本獲取它,對它進行修整以刪除由於某種原因似乎放在其前面的任何空間,然后使用LoadXML方法加載它。
您可以在此處查看供稿示例> http://scotjobsnet.co.uk.ni.strategiesuk.net/testfeed.xml
我也無法超越
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(feedURL);
或帶一個字符串。
XmlDocument xmlDoc = new XmlDocument();
string feedAsString = "";
// get from web as string
var webClient = new WebClient();
// Tell them who we are for white listing
webClient.Headers.Add("user-agent", "Mozilla/5.0 (compatible; Job Feed Importer;)");
// fetch feed as string
var content = webClient.OpenRead(feedURL);
var contentReader = new StreamReader(content);
var rssFeedAsString = contentReader.ReadToEnd();
rssFeedAsString = rssFeedAsString.Trim(); // remove any white space beore the feed
xmlDoc.LoadXml(feedAsString);
我得到的錯誤是:
Root element is missing.
Could not extract first items from feed string; Error The element with name 'jobs' and namespace '' is not an allowed feed format.
我想使用xpath / jobs / job /遍歷feed節點。
之前,我已經使用XmlDocument解析了這樣的提要,而XmlDocument只傳入了一個URL,如果沒有則傳入一個字符串。
我正在考慮使用正則表達式使用<job>[\\s\\S]+></job>
類型表達式在提要中循環。
但是我寧願使用標准方法。
由於無法更改提要,因此任何人都可以告訴我提要有什么問題以及我解析它的方式。 原諒var的使用,我只是被一小段代碼片段解析為使用該示例的提要。 我在其他任何地方都使用強類型,並且一旦工作就將其轉換。
任何幫助將非常感激。
謝謝
編輯:您當前的代碼失敗的原因很簡單-您正在嘗試解析一個空字符串:
string feedAsString = "";
...
var rssFeedAsString = contentReader.ReadToEnd();
rssFeedAsString = rssFeedAsString.Trim();
xmlDoc.LoadXml(feedAsString);
您永遠不會將feedAsString
設置為新值-但您會以rssFeedAsString
獲取文本。 那是兩個不同的變量。
也就是說,我將完全使用另一種方法。 我認為沒有必要進行修整等操作-使用XPath或將其通過RSS閱讀器傳遞(假設它不是 RSS)。 唯一棘手的部分是顯式指定User-Agent標頭,否則服務器將拒絕該請求。
我個人使用LINQ to XML,這似乎很好:
using System;
using System.Net;
using System.Xml.Linq;
class Test
{
static void Main()
{
string text;
using (var webClient = new WebClient())
{
string url = "http://scotjobsnet.co.uk.ni.strategiesuk.net/testfeed.xml";
webClient.Headers.Add("user-agent", "Mozilla/5.0");
text = webClient.DownloadString(url);
}
var doc = XDocument.Parse(text);
foreach (var job in doc.Root.Elements("job"))
{
Console.WriteLine(job);
}
}
}
聽起來很傻,請嘗試Html Agility Pack 。 它旨在處理格式不太正確的輸入,並且您可以使用類似XPath的表達式來遍歷樹。
這對我有用。 我使用了DownloadString。
var feedURL = "http://scotjobsnet.co.uk.ni.strategiesuk.net/testfeed.xml";
XmlDocument xmlDoc = new XmlDocument();
string feedAsString = "";
// get from web as string
var webClient = new WebClient();
// Tell them who we are for white listing
webClient.Headers.Add("user-agent", "Mozilla/5.0 (compatible; Job Feed Importer;)");
// fetch feed as string
var content = webClient.DownloadString(feedURL);
xmlDoc.LoadXml(content);
var jobs = xmlDoc.GetElementsByTagName("job");
foreach (var job in jobs)
{
//Loop through Jobs
}
我使用了以下解決方案,請看看:
XmlDocument xdoc = new XmlDocument();
xdoc.Load("http://scotjobsnet.co.uk.ni.strategiesuk.net/testfeed.xml");
if (xdoc != null)
{
XmlElement root = xdoc.DocumentElement;
XmlNodeList xNodelst = root.SelectNodes("job");
foreach (XmlNode node in xNodelst)
{
string location = node.SelectSingleNode("location").InnerText;
Response.Write("<br/> location = " + location);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.