This question already has answers here:
What is the best way to parse html in C#? [closed]
(15 answers)
Get a value of an attribute by HtmlAgilityPack
(5 answers)
HTML Agility Pack get all anchors' href attributes on page
(1 answer)
Closed 2 years ago.
I need to find the part of the string between the two tags :
First tag <h3><a href="/questions/
Last tag " class
I'm trying to use indexof and substring but what should I do with the substring ?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MyString
{
public partial class Form1 : Form
{
private string htmlCode = "";
public Form1()
{
InitializeComponent();
var myString = GetSources();
int index = myString.IndexOf("<h3><a href=") + 11;
string restulr = myString.Substring(index, )
// <h3><a href="/questions/
// " class
}
private string GetSources()
{
using (WebClient client = new WebClient()) // WebClient class inherits IDisposable
{
htmlCode = client.DownloadString("https://myStringTest.com");
}
return htmlCode;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Use RegEx to separate HTML or use the Html Agility Pack (HAP).
Related
Given the following string:
a:3:{s:6:"status";i:1;s:3:"msg";s:39:"Transaction has been already Completed.";s:6:"result";s:1620:"eyJtaWhwYXlpZCI6NDAzOTkzNzE1NTExODQwODY3LCJ tb2RlIjoiREJRUiIsInN0YXR1cyI6ImZhaWx1cmUiLCJrZXkiOiI3a2h6VEciLCJ0eG5pZCI6IjEy MzQ1IiwiYW1vdW50IjoiMS4wMCIsImFkZGVkb24iOiIyMDE5LTA1LTE3IDE3OjEwOjEzIiwic HJvZHVjdGluZm8iOiJPZmZsaW5lIER5bmFtaWMgUVIiLCJmaXJzdG5hbWUiOiIiLCJsYXN0 bmFtZSI6IiIsImFkZHJlc3MxIjoiIiwiYWRkcmVzczIiOiIiLCJjaXR5IjoiIiwic3RhdGUiOiIiLCJjb3V udHJ5IjoiIiwiemlwY29kZSI6IiIsImVtYWlsIjoiIiwicGhvbmUiOiIiLCJ1ZGYxIjoiIiwidWRmMiI6IiIsI nVkZjMiOiIiLCJ1ZGY0IjoiIiw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SE5MTc4MjcwNTc2MDQiLCJmaWVsZDciOiJ UcmFuc2FjdGlvbiBzdWNjZXNzIiwiZmllbGQ4IjoiIiwiZmllbGQ5IjoiU1VDQ0VTU3xDb21wbGV 0ZWQgVXNpbmcgQ2FsbGJhY2siLCJwYXltZW50X3NvdXJjZSI6InBheXUiLCJQR19UWVBF IjoiQlFSIiwiZXJyb3IiOiJFMDAwIiwiZXJyb3JfTWVzc2FnZSI6Ik5vIEVycm9yIiwibmV0X2Ftb3V udF9kZWJpdCI6MCwidW5tYXBwZWRzdGF0dXMiOiJkcm9wcGVkIiwiaGFzaCI6IjgxMWI0N TE2ODdhYzE5NjYwMTk4MjVjYjgzMjMzMGMyNzMyNjk5MTI0YzI4MWU5NGJlMDE1ODJh OTRiMzc5OWJkMjYyNWZlY2Y2NzViNDA4ZWUzZDk1ZTU4NzZmZTgyNWFmMDJlMGNh M2IzNGRlMDY1NzViMDJmMDgxZjg0OTllIiwiYmFua19yZWZfbm8iOiI5MTM3Mzk5NTMyMT YiLCJiYW5rX3JlZl9udW0iOiI5MTM3Mzk5NTMyMTYiLCJiYW5rY29kZSI6IlVQSUJRUiIsInN1 cmwiOm51bGwsImN1cmwiOm51bGwsImZ1cmwiOm51bGwsIm1lQ29kZSI6IntcInBnTWVyY 2hhbnRJZFwiOlwiSERGQzAwMDAwMDA2MTg5OFwifSJ9"
Using Regex :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string pattern = #"\{(?'bracket'.*)\}";
string input = "a:3:{s:6:\"status\";i:1;s:3:\"msg\";s:39:\"Transaction has been already Completed.\";s:6:\"result\";s:1620:\"eyJtaWhwYXlpZCI6NDAzOTkzNzE1NTExODQwODY3LCJ tb2RlIjoiREJRUiIsInN0YXR1cyI6ImZhaWx1cmUiLCJrZXkiOiI3a2h6VEciLCJ0eG5pZCI6IjEy MzQ1IiwiYW1vdW50IjoiMS4wMCI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0dXMiOiJkcm9wcGVkIiwiaGFzaCI6IjgxMWI0N TE2ODdhYzE5NjYwMTk4MjVjYjgzMjMzMGMyNzMyNjk5MTI0YzI4MWU5NGJlMDE1ODJh OTRiMzc5OWJkMjYyNWZlY2Y2NzViNDA4ZWUzZDk1ZTU4NzZmZTgyNWFmMDJlMGNh M2IzNGRlMDY1NzViMDJmMDgxZjg0OTllIiwiYmFua19yZWZfbm8iOiI5MTM3Mzk5NTMyMT YiLCJiYW5rX3JlZl9udW0iOiI5MTM3Mzk5NTMyMTYiLCJiYW5rY29kZSI6IlVQSUJRUiIsInN1 cmwiOm51bGwsImN1cmwiOm51bGwsImZ1cmwiOm51bGwsIm1lQ29kZSI6IntcInBnTWVyY 2hhbnRJZFwiOlwiSERGQzAwMDAwMDA2MTg5OFwifSJ9\";}";
string insideBrackets = Regex.Match(input, pattern).Groups["bracket"].Value;
string[] messages = insideBrackets.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
string output = messages.Where(x => x.StartsWith("s:1620:")).First();
}
}
}
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;
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 6 years ago.
I attempting to display some XML results based on a user submitted xml document and user submitted XPath syntax into a rich text box. My issue is a I keep getting the error:
An unhandled exception of type 'System.NullReferenceException' occurred in WindowsFormsApplication1.exe
Additional information: Object reference not set to an instance of an object.
Here is my code below.
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;
using System.Collections.Generic;
using System.ComponentModel;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "XML |*.xml";
if (ofd.ShowDialog() == DialogResult.OK)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(ofd.FileName);
richTextBox1.Text = xDoc.SelectSingleNode(textBox1.Text).InnerText;
}
}
}
}
Change this line
richTextBox1.Text = xDoc.SelectSingleNode(textBox1.Text).InnerText;
to
XmlNode node = xDoc.SelectSingleNode(textBox1.Text);
if(node != null)
richTextBox1.Text = node.InnerText;
And investigate why node is null.
Hi I am trying to create a RSS reader in visual C# express. I need to read the rss feed into a text box when the form loads. I have never worked with RSS feeds before and all the examples I have come across are done in visual studio and it seems that I cant use this:
(XmlReader reader = XmlReader.Create(Url))
This is what I have got so far. It doesn't work.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var s = RssReader.Read("http://ph.news.yahoo.com/rss/philippines");
textBox1.Text = s.ToString();
}
public class RssNews
{
public string Title;
public string PublicationDate;
public string Description;
}
public class RssReader
{
public static List<RssNews> Read(string url)
{
var webResponse = WebRequest.Create(url).GetResponse();
if (webResponse == null)
return null;
var ds = new DataSet();
ds.ReadXml(webResponse.GetResponseStream());
var news = (from row in ds.Tables["item"].AsEnumerable()
select new RssNews
{
Title = row.Field<string>("title"),
PublicationDate = row.Field<string>("pubDate"),
Description = row.Field<string>("description")
}).ToList();
return news;
}
}
I am not sure what I should do. Please help.
Well your code is working as expected, since you are returning a List of RSSNews items, but you are assigning it to the textbox in a wrong way. Doing textBox1.Text = s.ToString(); will give System.Collections.Generic.List.... as a result.
Your method is reading RssNews items from the dataset and return around 23 items against the feed. You need to iterate through these items and display its text in the textbox, or better if you may use GridView or similar control to show these results.
You may try the following code in your Main method:
var s = RssReader.Read("http://ph.news.yahoo.com/rss/philippines");
StringBuilder sb = new StringBuilder();
foreach (RssNews rs in s)
{
sb.AppendLine(rs.Title);
sb.AppendLine(rs.PublicationDate);
sb.AppendLine(rs.Description);
}
textBox1.Text = sb.ToString();
This will create a string for each item of RssNews and will display the result in textBox1.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Removing carriage return and new-line from the end of a string in c#
How do I remove the carriage return at the end of string.
I have already google out this but I cannot find the code I need.
This is my current code:
return s.Replace("\n", "").Replace("\r", "").Replace("\r\n", "").Trim();
but of course all the occurrence of the old character will be replaced.
I have tried these:
public string trim(string s)
{
string[] whiteSpace = {"\r", "\n", "\r\n"};
foreach(string ws in whiteSpace)
{
if (s.EndsWith(ws))
{
s = s.Substring(0, s.Length - 1);
}
}
return s;
}
but is not working for me, maybe I miss something or there's a wrong with my code
I have also try using regular expression but I can't get what I need.
I get the string from word document then will be transfered to another docs.
I'll appreciate any help.Tnx
Hi I found a solution in following link. So the credit should go to that author.
Example
However I have customized it to boost the demonstrative capability.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
String s = "ABCDE\r\n" + "FGHIJ";
MessageBox.Show(s);
MessageBox.Show(RemoveAndNewlineLineFeed(s));
}
string RemoveAndNewlineLineFeed(string s)
{
String[] lf = { "\r", "\n" };
return String.Join("",s.Split(lf,StringSplitOptions.RemoveEmptyEntries));
}
}
}
Check this out.
See this answer: Removing carriage return and new-line from the end of a string in c#