【发布时间】:2018-11-06 10:36:06
【问题描述】:
我正在尝试在 C# 应用程序中进行一些抓取。
我正在尝试访问以下页面上的 4 条信息: https://smstestbed.nist.gov/vds/current
- 创建时间
- 可用性
- 线性 X 和 Y 坐标
以下功能是我从远程加工工具轮询实时数据馈送的地方。 我遇到的问题是,虽然我能够将“CreationTime”打印到终端,但我的 XPath 使用非常笨拙,而且就This Link 而言,我似乎应该能够在两行中做我正在做的事情在我的评论之后
"//这应该是访问数据的更好方法,但由于某种原因第二行失败"
不幸的是,我得到 AvailabilityNode 为 Null。
public static void PollNIST()
{
string NISTSourceURL = "https://smstestbed.nist.gov/vds/current"; // Gives us a human friendly reference to the HTM
//-------------------------------- Current (mostly) Working Version---------------------------------------------------------------------------------
// Retrieve raw HTML
var NISTTargetURL = NISTSourceURL;
var NISTHttpClient = new HttpClient();
var NISTXMLRaw = NISTHttpClient.GetStringAsync(NISTTargetURL); // We now have all of the HTML / XML Data as a raw string
//Console.WriteLine(MazXMLRaw.Result); // Prints the resulting HTML to a terminal as a debug tool (Works)
XmlDocument CurNISTXML = new XmlDocument(); // Generate Blank XML Doc
CurNISTXML.LoadXml(NISTXMLRaw.Result); // This (".result") passes the actual string?, should then be loaded into new XML file
var elementHeader = CurNISTXML.GetElementsByTagName("Header");
var curNISTHeader = elementHeader.Item(0);
var creationTime = curNISTHeader.Attributes[0]; // We actually have the creationTime
string CurNISTTime = creationTime.InnerText; ; // //*[@id="mtconnect content"]/ul/li[1]
//This should be a far better way of accessing the data but for some reason the second line fails
XmlNode AvailabilityNode = CurNISTXML.SelectSingleNode("/table[1]/tbody/tr[1]"); //*[@id="mtconnect content"]/table[1]/tbody/tr[1]/td[7] // Xpath Availability
var CurNISTStatus = AvailabilityNode.InnerText; // //*[@id="mtconnect content"]/ul/li[1]
string CurNistX = ""; // //*[@id="mtconnect content"]/table[5]/tbody/tr/td[7]
string CurNistY = ""; // //*[@id="mtconnect content"]/table[6]/tbody/tr/td[7]
Console.WriteLine("-------BEGIN NIST DATA PACKET-------");
Console.WriteLine("NIST Time : " + creationTime.InnerText);
Console.WriteLine("NIST Status: " + CurNISTStatus);
Console.WriteLine("NIST X Pos.: " + CurNistX);
Console.WriteLine("NIST Y Pos.: " + CurNistY);
Console.WriteLine("--------END NIST DATA PACKET--------");
//var currentNIST = new NISTDataSet()// Create new instance ofNISTdata object
}
有什么想法吗?
【问题讨论】:
-
您正在尝试使用 xml 解析 html 网页。您使用了错误的 URL。数据以 XML 形式提供,但您需要使用不同的 URL。请参阅:nist.gov/programs-projects/materials-data-curation-system
-
你确定吗?如果我将 XML 文档打印到控制台,它就在那里,并且创建时间工作得很好。
-
这是我第一次写 c#,所以我被一些可能很简单的东西卡住了
-
您使用的是什么 xml 链接?你发布的只是html。
-
时间戳只能使用方法第一行给出的链接获取