Extract Specific Text from Html Page - c#

Html page is look like this
<tr>
<th rowspan="4" scope="row">General</th>
<td class="ttl">2G Network</td>
<td class="nfo">GSM 850 / 900 / 1800 / 1900 </td>
</tr><tr>
<td class="ttl">3G Network</td>
<td class="nfo">HSDPA 900 / 1900 / 2100 </td>
</tr>
for that i am try to use
var text = document.getElementsByClassName("nfo")[0].innerHTML;
Provided By Alex
But i am getting this error
Error 2 The name 'document' does not exist in the current context C:\Users\Nabi Javid\Documents\Visual Studio 2008\Projects\WpfApplication2\WpfApplication2\Window1.xaml.cs 30 22 WpfApplication2
Am i missing some Libary or something
Currently my code is like that
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.Load("nokia_c5_03-3578.html");
var text = document.getElementsByClassName("nfo")[0].innerHTML;
}
}
}

You are mixing C# code with javascript code.
Instead of this:
var text = document.getElementsByClassName("nfo")[0].innerHTML;
type this:
var text = htmlDoc.DocumentNode.SelectNodes("//td[#class='nfo']")[0].InnerHtml;
To keep it simple, I have refrained from checking exceptions.

I'm not very deep into .net but it looks like you are trying to mix JavaScript-code
var text = document.getElementsByClassName("nfo")[0].innerHTML;
with your .net code...?

You must use the htmlDoc variable to call methods in your case.
By the way the HtmlDocument class does not have a method with that name. Try to see if you can find another match for your needs in this list.
As the error says, the document variable does not exits in your code.

do you want
var text = htmlDoc.getElementsByClassName("nfo")[0].innerHTML;
? Not familiar with HTML Agility Pack, but that would seem to make sense

You can get elements by class name using next method which return elements where are several classes defined in one class attribute:
private HtmlNodeCollection GetElementsByClassName(HtmlDocument htmlDocument, string className)
{
string xpath =
String.Format(
"//*[contains(concat(' ', normalize-space(#class), ' '), ' {0} ')]",
className);
return htmlDocument.DocumentNode.SelectNodes(xpath);
}

Related

How to get HTMLElement ID if it includes a "-"

I am trying to load Google and get the ID of the searchbox. The ID of the box is "lst-ib". Which when the program goes to debug it is expecting a semicolon.
Is there a way around it to get the element id? So far I have:
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
public void Main(string[] args)
{
Process.Start("www.google.com");
HtmlElement lst-ib = WebBrowser1.Document.All["foo"];
//expects a semi colon on the line above after the element id
if (lst-ib != null)
{
lst-ib.InnerText = "test";
}
Console.ReadKey();
}
}
}
That is C# code and - is not valid in identifiers. Feel free to name the variable as you wish – it has no bearing on what the ID of the element is.
The - is an operator, you cannot use this way!
Here you will find more information about operators:
https://msdn.microsoft.com/en-us/library/6a71f45d.aspx
I recomend you rename - (trace) to _ (underline) or anyway you want
=D

How to fetch specific data from XML using C#

I'm trying to fetch specific data from a Xml file to display in a textbox. My Xml file name as "test.xml" has following code
<?xml version="1.0" encoding="utf-8" ?>
<Body>
<Context>
<PageNo>a87</PageNo>
<Verse>"Do it right"</Verse>
</Context>
</Body>
My C# code is below: Edited to reflect recent chnages
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
namespace learn2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
pageid1();
}
private void pageid1()
{
textBox1.Clear();
XmlDocument doc = new XmlDocument();
doc.Load("C:\\test.xml");
var pagenoNodeList = doc.SelectNodes("Body/Context/PageNo");
var pageNoNode = pagenoNodeList[0]; // To select the first node
var text = pageNoNode.InnerText; // Gets the text value inside the node
textBox1.Text = text;
} }
}
I highly appreciate any help. Thanks
You need to identify the node you are looking for, as SelectNodes returns a System.Xml.XmlNodeList, and then get the value of InnerText:
var pagenoNodeList = doc.SelectNodes("Body/Context/PageNo");
var pageNoNode = pagenoNodeList[0]; // To select the first node
var text = pageNoNode.InnerText; // Gets the text value inside the node
textBox1.Text = text;

how to get cpu state in C#

I am new to C# and programming in general.
I have created a simple plc programme (step7), simulated on PLSIM simulator.
and i want to control this programme with a WPF Interface in C# using Siemens S7ProSim COM Object reference.
here is the problem:
when i want to assign the CPU State to my label, this message is shown:
Error 1 'System.Windows.Controls.Label' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'System.Windows.Controls.Label' could be found (are you missing a using directive or an assembly reference?) C:\Users\Lenovo\Documents\Visual Studio 2010\Projects\CSProject\CSProject\MainWindow.xaml.cs 32 28 CSProject
here is my proramm and my wpf interface:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace CSProject
{
public partial class MainWindow : Window
{
public S7PROSIMLib.S7ProSim ps = new S7PROSIMLib.S7ProSim();
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
ps.Connect();
label_CPUState.Text = ps.GetState();
labelScanMode.Text = ps.GetScanMode().ToString();
}
}
}
Thanks a lot.
I believe you want the Content property of the Label, not the Text property. Text is Winforms.
So, simply:
labelScanMode.Content = ps.GetScanMode().ToString();

How to get grid items --wpf

I made WPF application named as WpfApplication7.It has several folders. One of them is named as Assign_Rights . I have created a UserControl in this folder and named it as Assignment_Rights. Inside this user control i have created a grid and bind it to data explicitly! Now There is button name as Click New. It shows me that PK(Primary Key-Value) of a selected item from grid.
User n = grid.GetRow(view.FocusedRowHandle) as User;
int abc = n.Primary_Key;
Question: As i wrote code to get PK of Selected item it generated error at
int abc = n.Primary_Key;
by saying
Error 'WpfApplication7.Assign_Rights.User' does not contain a definition for 'Primary_Key' and no extension method 'Primary_Key' accepting a first argument of type 'WpfApplication7.Assign_Rights.User' could be found (are you missing a using directive or an assembly reference?) D:\Backup_WPFAppliacation7\New folder\WpfApplication7\WpfApplication7\Assign_Rights\Assignment_Rights.xaml.cs 59 28 WpfApplication7
How to Solve this Error?
EDIT
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DevExpress.Xpf.Core;
using DevExpress.Xpf.Grid;
namespace WpfApplication7.Assign_Rights
{
public partial class Assignment_Rights : UserControl
{
int useridd;
nrcsaEntities d = new nrcsaEntities();
public Assignment_Rights()
{
InitializeComponent();
}
private void ToggleButton1_Click_1(object sender, RoutedEventArgs e)
{
if (view.IsRowSelected(view.FocusedRowHandle) == false)
{
DXMessageBox.Show("Please Select any Item From Grid List");
}
else
{
User n = grid.GetRow(view.FocusedRowHandle) as User;
int abc = n.Primary_Key;
var getd = from p in d.Users select p;
MessageBox.Show(view.FocusedRowHandle.ToString());
} . . . .

Append Xml a given number of times

I am appending to an existing xml file: The code is below: I am using C# .net 4.5 VS 2012 and creating a WPF application.
How could I append this say 30 times and only change D100 attribute number to 2,3,4,5 etc?
The other values are the same!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
namespace AppendX
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load("C:\\Temp.xml");
XmlNamespaceManager namespaces = new XmlNamespaceManager(doc.NameTable);
namespaces.AddNamespace("flp", "http://www.w3.org/2001/flp");
XmlNode nextNode = doc.SelectSingleNode("/flp:Tab/flp:Designs", namespaces);
XmlElement D100 = doc.CreateElement("flp", "D100", "http://www.w3.org/2001/flp");
D100.SetAttribute("Number", "2");
XmlElement Code = doc.CreateElement("flp", "Code", "http://www.w3.org/2001/flp");
Code.InnerText = "B";
D100.AppendChild(Code);
XmlElement Documented = doc.CreateElement("flp", "Documented", "http://www.w3.org/2001/flp");
Documented.InnerText = "false";
D100.AppendChild(Documented);
nextNode.AppendChild(D100);
doc.Save("test1.xml");
}
}
}
Here is the sample xml I am using, sorry I meant to put this up!
<flp:Tab xmlns:flp="http://www.w3.org/2001/flp" Title="Testing">
<flp:Form Number="0" id="1005" />
<flp:Rev Time="2013-01-21T15:08:00">
<flp:Author Name="Brad" Aid="15" />
</flp:Rev>
<flp:Designs Id="D100">
<flp:D100 Number="1">
<flp:Code>A</flp:Code>
<flp:Documented>true</flp:Documented>
</flp:D100>
</flp:Designs>
</flp:Tab>
Create a separate private function that would handle the creation of the document element, given the parameters. ex:
private xmlelement dothework(string param1, string param2){
'do all necessary work to set up the element in here and then return it
}
I would separate the work like this, create a different function for each section of work, so that eventually you can just loop through and append each to the document.

Categories