XML Reader C# - 无法从 object 获取所有元素
XML Reader C# - cannot get all elements from object
我正在创建一个桌面应用程序,它从 eBay RSS 提要中提取一些 XML。我可以获得列表标题和 link 但我无法从中获取 CurrentPrice 元素。我正在使用 XmlDocument 在 C# 中工作。
这是 XML 文件的一个片段。
<rss xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005" xmlns:e="http://www.ebay.com/marketplace/search/v1/services" version="2.0">
<channel>
<cf:listinfo>
<cf:group ns="http://www.ebay.com/marketplace/search/v1/services" label="listing format" element="ListingType" data-type="number"/>
<cf:group ns="http://www.ebay.com/marketplace/search/v1/services" label="option" element="PaymentMethod" data-type="number"/>
<cf:sort ns="http://www.ebay.com/marketplace/search/v1/services" label="List Order" element="ListOrder" data-type="number"/>
<cf:sort ns="http://www.ebay.com/marketplace/search/v1/services" label="No of bids" element="BidCount" data-type="number"/>
<cf:sort ns="http://www.ebay.com/marketplace/search/v1/services" label="Current auction price" element="CurrentPrice" data-type="number"/>
<cf:sort ns="http://www.ebay.com/marketplace/search/v1/services" label="Buy It Now price" element="BuyItNowPrice" data-type="number"/>
<cf:sort ns="http://www.ebay.com/marketplace/search/v1/services" label="Listing end time" element="ListingEndTime" data-type="number"/>
</cf:listinfo>
<title> </title>
<link>#</link>
<subtitle>
Customize as you please by changing the URL. The keyword before the .atom / .rss extension determines the result that is displayed
</subtitle>
<item>
<title>
Rimmel London Lasting Finish Soft Colour Blush Blusher 020 PINK ROSE
</title>
<description>
<![CDATA[
<table border='0' cellpadding='8'> <tr><td> <a href= 'http://rover.ebay.com/rover/1/710-53481-19255-0/1?ff3=2&toolid=10039&campid=5338271107&item=264156575558&vectorid=229508&lgeo=1' target='_blank'><img src='http://thumbs3.ebaystatic.com/m/mdy_hoHsem7RVhXXqL-3-ZA/140.jpg' border='0'/></a></td><td><strong>£3.75</strong><br>End Date: Thursday Feb-21-2019 10:28:06 GMT<br>Buy It Now for only: £3.75<br><a href='http://rover.ebay.com/rover/1/710-53481-19255-0/1?ff3=2&toolid=10039&campid=5338271107&item=264156575558&vectorid=229508&lgeo=1' target='_blank'>Buy It Now</a> | <a href='http://rover.ebay.com/rover/1/710-53481-19255-0/1?ff3=4&toolid=10039&campid=5338271107&vectorid=229508&lgeo=1&mpre=http%3A%2F%2Fcgi1.ebay.com%2Fws%2FeBayISAPI.dll%3FMfcISAPICommand%3DMakeTrack%26item%3D264156575558%26ssPageName%3DRSS%3AB%3ASRCH%3AUS%3A104' target='_blank'>Add to watch list</a></td></tr> </table>
]]>
</description>
<pubDate>2019-01-22T10:28:06.000Z</pubDate>
<guid>264156575558</guid>
<link>
http://rover.ebay.com/rover/1/710-53481-19255-0/1?ff3=2&toolid=10039&campid=5338271107&item=264156575558&vectorid=229508&lgeo=1
</link>
<e:EekStatus/>
<e:BidCount/>
<e:CurrentPrice>3.75</e:CurrentPrice>
<e:ListingType>StoreInventory</e:ListingType>
<e:BuyItNowPrice/>
<e:ListingEndTime>2019-02-21T10:28:06.000Z</e:ListingEndTime>
<e:ListOrder>264156575558</e:ListOrder>
<e:PaymentMethod>PayPal</e:PaymentMethod>
</item>
<item>
不同的是,link下的元素是带有前缀的,我无法访问它们。不幸的是,我对 XML 的了解非常有限。
这是我 运行.
的代码片段
XmlDocument rssXmlDoc = new XmlDocument();
rssXmlDoc.Load(url);
// Parse the Items in the RSS file
XmlNodeList rssNodes = rssXmlDoc.SelectNodes("rss/channel/item");
foreach(XmlNode xn in rssNodes)
{
Console.WriteLine(xn.Name);
}
StringBuilder rssContent = new StringBuilder();
List<object> ebayList = new List<object>();
// Iterate through the items in the RSS file
foreach (XmlNode rssNode in rssNodes)
{
OleDbConnection conn;
conn = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data Source=E:\Development\ebay\ebay\ebay.mdb");
conn.Open();
OleDbCommand cmd = conn.CreateCommand();
XmlNode rssSubNode = rssNode.SelectSingleNode("title");
string title = rssSubNode != null ? rssSubNode.InnerText : "";
rssSubNode = rssNode.SelectSingleNode("link");
string link = rssSubNode != null ? rssSubNode.InnerText : "";
rssSubNode = rssNode.SelectSingleNode("description");
string description = rssSubNode != null ? rssSubNode.InnerText : "";
rssSubNode = rssNode.SelectSingleNode("CurrentPrice");
string currentPrice = rssSubNode != null ? rssSubNode.InnerText : "";
Console.Write(currentPrice);
其他元素 return 值非常好 - 但不是 CurrentPrice。
希望这些信息足够有人帮助我。
在我这边,它可以进行以下更改。
开头,之后
XmlDocument rssXmlDoc = new XmlDocument();
rssXmlDoc.Load(url);
你加
XmlNamespaceManager ns = new XmlNamespaceManager(rssXmlDoc.NameTable);
ns.AddNamespace("e", "http://www.ebay.com/marketplace/search/v1/services");
(匹配 xmlns:e="http://www.ebay.com/marketplace/search/v1/services"
)。
模式读取通过以下方式完成:
rssSubNode = rssNode.SelectSingleNode("e:CurrentPrice", ns);
解释:e
是一个命名空间,所以CurrentPrice
与e:CurrentPrice
不同。为了解释 XmlDocument
,您需要哪个命名空间,您创建了一个命名空间管理器并在其中注册了您的命名空间。
适当的 MS 文档条目:https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmlnode.selectsinglenode?view=netframework-4.7.2
我正在创建一个桌面应用程序,它从 eBay RSS 提要中提取一些 XML。我可以获得列表标题和 link 但我无法从中获取 CurrentPrice 元素。我正在使用 XmlDocument 在 C# 中工作。
这是 XML 文件的一个片段。
<rss xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005" xmlns:e="http://www.ebay.com/marketplace/search/v1/services" version="2.0">
<channel>
<cf:listinfo>
<cf:group ns="http://www.ebay.com/marketplace/search/v1/services" label="listing format" element="ListingType" data-type="number"/>
<cf:group ns="http://www.ebay.com/marketplace/search/v1/services" label="option" element="PaymentMethod" data-type="number"/>
<cf:sort ns="http://www.ebay.com/marketplace/search/v1/services" label="List Order" element="ListOrder" data-type="number"/>
<cf:sort ns="http://www.ebay.com/marketplace/search/v1/services" label="No of bids" element="BidCount" data-type="number"/>
<cf:sort ns="http://www.ebay.com/marketplace/search/v1/services" label="Current auction price" element="CurrentPrice" data-type="number"/>
<cf:sort ns="http://www.ebay.com/marketplace/search/v1/services" label="Buy It Now price" element="BuyItNowPrice" data-type="number"/>
<cf:sort ns="http://www.ebay.com/marketplace/search/v1/services" label="Listing end time" element="ListingEndTime" data-type="number"/>
</cf:listinfo>
<title> </title>
<link>#</link>
<subtitle>
Customize as you please by changing the URL. The keyword before the .atom / .rss extension determines the result that is displayed
</subtitle>
<item>
<title>
Rimmel London Lasting Finish Soft Colour Blush Blusher 020 PINK ROSE
</title>
<description>
<![CDATA[
<table border='0' cellpadding='8'> <tr><td> <a href= 'http://rover.ebay.com/rover/1/710-53481-19255-0/1?ff3=2&toolid=10039&campid=5338271107&item=264156575558&vectorid=229508&lgeo=1' target='_blank'><img src='http://thumbs3.ebaystatic.com/m/mdy_hoHsem7RVhXXqL-3-ZA/140.jpg' border='0'/></a></td><td><strong>£3.75</strong><br>End Date: Thursday Feb-21-2019 10:28:06 GMT<br>Buy It Now for only: £3.75<br><a href='http://rover.ebay.com/rover/1/710-53481-19255-0/1?ff3=2&toolid=10039&campid=5338271107&item=264156575558&vectorid=229508&lgeo=1' target='_blank'>Buy It Now</a> | <a href='http://rover.ebay.com/rover/1/710-53481-19255-0/1?ff3=4&toolid=10039&campid=5338271107&vectorid=229508&lgeo=1&mpre=http%3A%2F%2Fcgi1.ebay.com%2Fws%2FeBayISAPI.dll%3FMfcISAPICommand%3DMakeTrack%26item%3D264156575558%26ssPageName%3DRSS%3AB%3ASRCH%3AUS%3A104' target='_blank'>Add to watch list</a></td></tr> </table>
]]>
</description>
<pubDate>2019-01-22T10:28:06.000Z</pubDate>
<guid>264156575558</guid>
<link>
http://rover.ebay.com/rover/1/710-53481-19255-0/1?ff3=2&toolid=10039&campid=5338271107&item=264156575558&vectorid=229508&lgeo=1
</link>
<e:EekStatus/>
<e:BidCount/>
<e:CurrentPrice>3.75</e:CurrentPrice>
<e:ListingType>StoreInventory</e:ListingType>
<e:BuyItNowPrice/>
<e:ListingEndTime>2019-02-21T10:28:06.000Z</e:ListingEndTime>
<e:ListOrder>264156575558</e:ListOrder>
<e:PaymentMethod>PayPal</e:PaymentMethod>
</item>
<item>
不同的是,link下的元素是带有前缀的,我无法访问它们。不幸的是,我对 XML 的了解非常有限。
这是我 运行.
的代码片段 XmlDocument rssXmlDoc = new XmlDocument();
rssXmlDoc.Load(url);
// Parse the Items in the RSS file
XmlNodeList rssNodes = rssXmlDoc.SelectNodes("rss/channel/item");
foreach(XmlNode xn in rssNodes)
{
Console.WriteLine(xn.Name);
}
StringBuilder rssContent = new StringBuilder();
List<object> ebayList = new List<object>();
// Iterate through the items in the RSS file
foreach (XmlNode rssNode in rssNodes)
{
OleDbConnection conn;
conn = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data Source=E:\Development\ebay\ebay\ebay.mdb");
conn.Open();
OleDbCommand cmd = conn.CreateCommand();
XmlNode rssSubNode = rssNode.SelectSingleNode("title");
string title = rssSubNode != null ? rssSubNode.InnerText : "";
rssSubNode = rssNode.SelectSingleNode("link");
string link = rssSubNode != null ? rssSubNode.InnerText : "";
rssSubNode = rssNode.SelectSingleNode("description");
string description = rssSubNode != null ? rssSubNode.InnerText : "";
rssSubNode = rssNode.SelectSingleNode("CurrentPrice");
string currentPrice = rssSubNode != null ? rssSubNode.InnerText : "";
Console.Write(currentPrice);
其他元素 return 值非常好 - 但不是 CurrentPrice。
希望这些信息足够有人帮助我。
在我这边,它可以进行以下更改。
开头,之后
XmlDocument rssXmlDoc = new XmlDocument();
rssXmlDoc.Load(url);
你加
XmlNamespaceManager ns = new XmlNamespaceManager(rssXmlDoc.NameTable);
ns.AddNamespace("e", "http://www.ebay.com/marketplace/search/v1/services");
(匹配 xmlns:e="http://www.ebay.com/marketplace/search/v1/services"
)。
模式读取通过以下方式完成:
rssSubNode = rssNode.SelectSingleNode("e:CurrentPrice", ns);
解释:e
是一个命名空间,所以CurrentPrice
与e:CurrentPrice
不同。为了解释 XmlDocument
,您需要哪个命名空间,您创建了一个命名空间管理器并在其中注册了您的命名空间。
适当的 MS 文档条目:https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmlnode.selectsinglenode?view=netframework-4.7.2