Everything you asked for can be done relatively easily. It does not matter that its documentation may be missing, it should be similar to the XML implementation and the XmlDocument
network, both in use and in functionality.
How to read values inside a specific table? Let's say there are 10 tables, and I want to read the values from the 6th, or I have a table identifier.
Search for the sixth table:
// XPath var table6 = doc.DocumentNode.SelectSingleNode("//table[6]"); // LINQ var table6 = doc.DocumentNode.Descendants("table").Skip(5).FirstOrDefault();
Search table / element by id:
var myTable = doc.GetElementById("myTable"); // XPath var myTable = doc.DocumentNode.SelectSingleNode("//table[@id='myTable']"); var myTable = doc.DocumentNode.SelectSingleNode("//*[@id='myTable']"); // LINQ var myTable = doc.DocumentNode .Descendants("table") .Where(table => table.Attributes.Contains("id")) .SingleOrDefault(table => table.Attributes["id"].Value == "myTable"); var myTable = doc.DocumentNode .Descendants() .Where(e => e.Attributes.Contains("id")) .SingleOrDefault(e => e.Attributes["id"].Value == "myTable"); var myTable = doc.DocumentNode .Descendants("table") .SingleOrDefault(table => table.GetAttributeValue("id", null) == "myTable"); var myTable = doc.DocumentNode .Descendants() .SingleOrDefault(e => e.GetAttributeValue("id", null) == "myTable");
Say I want to read the value of td after a specific td.
// XPath var certainTd = table6.SelectSingleNode("//td[2]"); var tdAfterCertainTd = certainTd.SelectSingleNode("following-sibling::td[1]"); // LINQ (not so easy) var certainTd = table6.Descendants("td").Skip(1).FirstOrDefault(); var tdAfterCertainTd = certainTd.NextSibling; while (tdAfterCertainTd != null) { if (tdAfterCertainTd.Name == "td") break; tdAfterCertainTd = tdAfterCertainTd.NextSibling; }
A table comes after a specific div or element or text.
// XPath var certainDiv = doc.DocumentNode.SelectSingleNode("//div[1]"); var tableAfterCertainDiv = certainDiv.SelectSingleNode("following-sibling::table[1]"); // LINQ (not so easy) var certainDiv = doc.DocumentNode.Descendants("div").FirstOrDefault(); var tableAfterCertainDiv = certainDiv.NextSibling; while (tableAfterCertainDiv != null) { if (tableAfterCertainDiv.Name == "table") break; tableAfterCertainDiv = tableAfterCertainDiv.NextSibling; }
You should notice some patterns.
source share