Posts

Showing posts with the label XPath

Donate

Read Or Parse XML Using XPathDocument In C#

Good day! In this example, I will show you how to read an XML file using XPathNavigator and XPathDocument classes in .NET. When using these classes, make sure to include the System.Xml and System.Xml.XPath namespaces in your file. XML File <?xml version="1.0" encoding="utf-8" ?> <Invoice xmlns:inv= "http://salesorgchart.abcdefg.com" > <inv:Customers> <inv:TotalSales>134.49</inv:TotalSales> -<inv:Customer> <inv:Date>2013-11-15</inv:Date> <inv:Number>10001</inv:Number> <inv:CustomerName>Cherry Pie</inv:CustomerName> <inv:PONumber>30002</inv:PONumber> <inv:Address>Cebu City Philippines</inv:Address> -<inv:Products> -<inv:Product> <inv:Code>AE445</inv:Code> <inv:Category>Liquid Milk</inv:Category> <inv:Name>Devondale</inv:Name>

Parse Html Table Using HTML Agility Pack In C#

Below is a simple code to parse a table using HTML Agility Pack. Make sure to add the Html Agility Pack package from Nuget and reference that library in the namespace of your program. 1 using HtmlAgilityPack; Parse Table 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 HtmlWeb web = new HtmlWeb(); HtmlDocument doc = web.Load( "http://your_sample_url" ); // Get all tables in the document HtmlNodeCollection tables = doc.DocumentNode.SelectNodes( "//table" ); // Iterate all rows in the first table HtmlNodeCollection rows = tables[0].SelectNodes( "tr" ); for ( int i = 0; i <= rows.Count - 1; i++) { // Iterate all columns in this row HtmlNodeCollection cols = rows[i].SelectNodes( "td" ); if (cols != null ) { for ( int j = 0; j <= cols.Count - 1; j++) { // Get the value of the column and print it string value = cols[j].InnerText; Console.Write

Read Or Parse XML Using XmlElement Class, XPath And XmlNodelist In C#

Sample xml file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <?xml version="1.0" encoding="UTF-8"?> <jobs> <job> <title> Payroll Analyst </title> <description> <summary> Seeking Payroll Analyst specialists. Great benefits and competitive salary. Must meet all requirements to be considered for assignment. </summary> </description> <location> <state> Pennsylvenia </state> </location> </job> <job /> </jobs> Code: 1 2 3 4 5 6 7 8 9 10 11 ListingSource = webclient.DownloadString( "your_xmlurl" ); StringReader readString = new System.IOStringReader(ListingSource); XmlDocument awesome = new XmlDocument(); awesome.Load(readString); XmlNodeList nodeList = awesome.SelectNodes( "//jobs/job" ); foreach (XmlElement element in nodeList) { title = elem

Donate