Actually I am kind a new at C# and I am trying to make a basic program for friendfeed for learn.
But I have a problem.
I can use C# library of friendfeed but it's old. For API V2 I have to work without library.
So I decide to do it without lib.
I use WebRequest first and I am getting .xml file always. ( I am saving it as string, you can see in code. )
I am just asking, what can I do know, how can I get informations inside a XML file.
Can you help me?
Thank You.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
class feed { }
static void Main(string[] args)
{
string username = "semihmasat";
WebRequest ffreq = WebRequest.Create("http://friendfeed-api.com/v2/feed/" + username + "?format=xml");
WebResponse ffresp = ffreq.GetResponse();
Console.WriteLine(((HttpWebResponse)ffresp).StatusDescription);
Stream stream = ffresp.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string respfinal = reader.ReadToEnd();
reader.Close();
Console.ReadLine();
}
}
}
You're looking for LINQ to XML.
You can also retrieve XML Data using Dataset.
Just write the code
Dataset ds = new Dataset();
ds.ReadXml(new MemoryStream(ASCIIEncoding.ASCII.GetBytes(respfinal )));
And get the data in tables and do whatever you required.
Related
I have the following XML in an API
I wish to read the data from the tags and build a CSV file with the values.
My code so far,
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using System.Net;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string url = "https://localhost:5001/api/Scheduler/GetScheduler";
WebRequest request = WebRequest.Create(url);
try
{
WebResponse response = request.GetResponse();
using (var sr = new System.IO.StreamReader(response.GetResponseStream()))
{
XDocument xmlDoc = new XDocument();
try
{
xmlDoc = XDocument.Parse(sr.ReadToEnd());
Console.WriteLine(xmlDoc.Root.Element("ORD_NAME").Value);
}
catch (Exception)
{
// handle if necessary
}
}
}
catch (WebException)
{
// handle if necessary
}
}
}
}
I can see the data being read in, but xmlDoc, but xmlDoc.Root.Element("ORD_NAME").Value is NULL.
How do I get the data from the stream?
Thanks.
I think you want e.g
using (var stream = response.GetResponseStream())
{
XDocument xmlDoc = XDocument.Load(stream);
Console.WriteLine(string.Join("\n", from row in xmlDoc.Root.Elements("Scheduler") select string.Join(";", row.Elements().Select(e => e.Value))));
}
.NET probably has some better APIs to construct CSVs than using LINQ to XML directly, the above will fail to quote value or escape separators.
I am trying to scrape data from this webpage.
Can some one help me solve this and get the HTML tags or get the XML or JSON? I think it's encoded with something other than gzip.
Here is my code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.tsetmc.com/Loader.aspx?ParTree=15");
using (Stream stream = request.GetResponse().GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
string response = reader.ReadToEnd();
}
}
}
}
}
Usually site like the one you linked, after loading the raw html ( that is what you obtain with the call you shown in your code ) launch many other Ajax call to fill various information on the page. You need to study these call in order to understand how to fetch the data. For example you can monitor the network traffic with F12 on Chrome and look at the Network tab.
My console program consists of a generic List with 4 values already in it when the console starts. The user can only input one more name into the list. I want my program to be able to serialize the data before closing. And I am able to deserialize from the xml file to print out all five names when the program start. The serialize part works properly. And I can also deserialize if I put my deserialize code right below the serialize part. But that defeats the purpose of being able to load your data from previous usage. Below is my code. Any help is appreciated.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace serializationtest
{
class Program
{
static void Main(string[] args)
{
List<string> ListOfName = new List<string>();
ListOfName.Add("Bob");
ListOfName.Add("Mary");
ListOfName.Add("Peter");
ListOfName.Add("May");
ListOfName.Add(null);
Console.WriteLine("Fill in the last name");
ListOfName[4] = Console.ReadLine();
XmlSerializer ListOfNameSerializer = new XmlSerializer(typeof(List<string>));
TextWriter FileSaveListOfName = new StreamWriter(#"ListofName.xml");
ListOfNameSerializer.Serialize(FileSaveListOfName, ListOfName);
FileSaveListOfName.Close();
XmlSerializer ListOfNameDeserializer = new XmlSerializer(typeof(List<string>));
FileStream ReadTheList = new FileStream(#"ListofName.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
ListOfName = (List<string>)ListOfNameSerializer.Deserialize(ReadTheList);
Console.ReadLine();
}
}
}
I want to read in one file and write to certain columns in the other
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
try
{
using (StreamWriter sw = new StreamWriter("AEJoinerDataTemplate.csv"))
// using streamwriter to write to a text file
using (StreamReader sr = new StreamReader("AutoEnrolment.csv"))
// using streamreader to read a text file
{
while ((txtline = sr.ReadLine()) != null)
{
A very flexible way would be to first put your records into objects and then write those fields of the objects you would like to output to a new file.
You could for example use the library below to read objects from a file and write objects to a file.
FileHelpers
Today in my code im downloading the images from a website like this:
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 HtmlAgilityPack;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using System.Net;
using System.Web;
using System.Threading;
using DannyGeneral;
using GatherLinks;
namespace GatherLinks
{
class RetrieveWebContent
{
HtmlAgilityPack.HtmlDocument doc;
string imgg;
int images;
public RetrieveWebContent()
{
images = 0;
}
public List<string> retrieveImages(string address)
{
try
{
doc = new HtmlAgilityPack.HtmlDocument();
System.Net.WebClient wc = new System.Net.WebClient();
List<string> imgList = new List<string>();
doc.Load(wc.OpenRead(address));
HtmlNodeCollection imgs = doc.DocumentNode.SelectNodes("//img[#src]");
if (imgs == null) return new List<string>();
foreach (HtmlNode img in imgs)
{
if (img.Attributes["src"] == null)
continue;
HtmlAttribute src = img.Attributes["src"];
imgList.Add(src.Value);
if (src.Value.StartsWith("http") || src.Value.StartsWith("https") || src.Value.StartsWith("www"))
{
images++;
string[] arr = src.Value.Split('/');
imgg = arr[arr.Length - 1];
wc.DownloadFile(src.Value, #"d:\MyImages\" + imgg);
}
}
return imgList;
}
catch
{
Logger.Write("There Was Problem Downloading The Image: " + imgg);
return null;
}
}
}
}
But sometimes in many cases the images are behind or under java script and cant be downloaded regular. How can i get/download the images and/or the whole complete website content including images and everything so later on in my hard disk i will have the complete website with all its content tree so i can surf to it Offline.
I would use an actual browser and then save images from there.. Take a look at Watir Webdriver for a solution in Ruby. This library helps you automate a browser... I would use it in combination with Nokogiri to achieve what you are trying to do above..
Python equivalents also exist..
Webdriver does not yet support the save functionality but the older "Watir" does. You might also want to look into CasperJS which provides some browser automation in the Javascript language.