I am creating an app for windows phone and i am searching the lyrics and need to display the data from the xml.
Now i know how to do this for many items in listbox,
but the xml data i receive is only ever going to have one option.
http://lyrics.wikia.com/api.php?artist=pharrell%20williams&song=happy&fmt=xml
There is an example of the xml i am trying to parse and display.
So any idea/ tips on how i would go around just parsing this one entry and displaying it to the textbox.
the only thing i am after is the lyrics data and the URL as that's all i will be displaying on the page.
You can use linq-to-xml to query specific information from xml. Following is an example to get lyrics and url using linq-to-xml :
var doc = XDocument.Parse(xml);
var lyrics = doc.Root.Element("lyrics").Value;
var url = doc.Root.Element("url").Value;
With that, lyrics and url information extracted and ready to be displayed in textbox or any other control of choice.
Note: xml is xml string downloaded from link in the question.
Related
Imagine, you are confronted with a big textfile, for example HTML, and only want to edit one line of code in this file.
The standard approach would be to first read everthing and then write everthing, including the changed text, back to a separate file, which would not be very efficient in this usecase.
I want to use a similar approach to open a file in the Editor, find the line you need and then edit this specific line.
Is there any FileIO that allows actual editing/replacing in stead of plain append or create?
EDIT:
What I have in my mind so far is exactly the example that Rahul Singh gave below.
But as mentioned, if I think about this approach it doesn't seem very efficient if you just want to edit one or even a few lines. In my actual problem where the question came from, the file is a HTML file in which want to insert a additional table row. But I think this use-case also is interesting to all files that contains plain text
You can use HTML Agility Pack.
This is an agile HTML parser that builds a read/write DOM and supports
plain XPATH or XSLT.It is a .NET code library that allows you to parse "out of the web"
HTML files.
For example this code fix all hrefs in HTML file:
HtmlDocument doc = new HtmlDocument();
doc.Load("file.htm");
foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[#href"])
{
HtmlAttribute att = link["href"];
att.Value = FixLink(att); //FixLink() is your custom method
}
doc.Save("file.htm");
firstly please share what you have tried so far, as we dont have any idea upon data that your textFile contains,
First read through out the file in streamreader and store it into string then do string.replace and do the editing, you could also use the split options and by check it by contains function do the editing.
and you can always go by XSLT options but for that you need to use XPath to select
I don't know what it called, but i think this is possible
I am looking to write something(don't know the exact name) that will,
go to a webpage and select a value from drop-down box on that page and read values from that page after selection, I am not sure weather it called crawler or activity, i am new to this but i heard long time back from one of my friend this can be done,
can any one please give me a head start
Thanks
You need an HTTP client library (perhaps libcurl in C, or some C# wrapper for it, or some native C# HTTP client library like this).
You also need to parse the retrieved HTML content. So you probably need an HTML parsing library (maybe HTML agility pack).
If the targeted webpage is nearly fixed and has e.g. some comments to ease finding the relevant part, you might use simpler or ad-hoc parsing techniques.
Some sites might send a nearly empty static HTML client, with the actual page being dynamically constructed by Javascript scripts (Ajax). In that case, you are unlucky.
Maybe you want some web service ....
One simple way (but not the most efficient way) is to simply read the webpage as String using the WebClient, for example:
WebClient Web = new WebClient();
String Data = Web.DownloadString("Address");
Now since HTML is simply an XML document you can parse the string to a XDocument and look up the tag that represents the dropdown box. Parsing the string to XDocument is done this way:
XDocument xdoc = XDocument.Pase(Data);
Update:
If you want to read the result of the selected value, and that result is displayed within the page do this:
Get all the items as I explained.
If the page does not make use of models, then you can use your selected value as an argument for example :
www.somepage.com/Name=YourItem?
Read the page again and find the value
My Program: Contains two textBoxes and one XML file on the desktop.
XML File:
My Goal: I want to parse XML File and display the desired values in the textBox. First number 123456 from XML File will be displayed in the first texBox. Similarly, Second number 9876 from XML File will be displayed in the second texBox. Rest, everything should be ignored in the XML File.
I tried to Google search for a simple example that will help me understand how to parse XML file and display in textbox but I couldn't find any such simple program example. This program will help me learn this technique quickly and easily, and your help will be much appreciated! Thanks!! :)
var dict = XDocument.Load(fname)
.Descendants("field")
.ToDictionary(f => f.Attribute("name").Value,
f => f.Attribute("value").Value);
firstNumberTextBox.Text = dict["first_number"];
secondNumberTextBox.Text = dict["second_number"];
I am trying to get the most recent NWS radar images using c#. There are directories on the NWS website that contain a list of the most recent images. However, the files are named by the date uploaded, not in numerical order. They are generally uploaded every few minutes, but the exact amount of minutes can vary by as much as 5 minutes. To get the URLs of the images, I could write an XML parser to extract the URLs from the index page, however this seems over complicated for such a simple task. In addition, this index page is not an API, and if they might change something with the format that would screw up the XML parser. Is there some other way to get the URLs of the most recent images?
An html is not always a valid Xml. But you can use use a real html parser like HtmlAgilityPack for this.
WebClient wc = new WebClient();
var page = wc.DownloadString("http://radar.weather.gov/ridge/RadarImg/NCR/OKX/?C=M;O=D");
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(page);
var imageLink = doc.DocumentNode.SelectNodes("//td/a[#href]")
.Select(a=>a.Attributes["href"].Value)
.OrderByDescending(a=>a)
.First();
--EDIT--
Forget about this answer and go that way United States Weather Radar Data Feed or API?
I'm trying to replace a text element placeholder with an image in an openXML docx.
I've found a tutorial here which seems to do what I need, but I'm not quite following what he does to insert the image.
Basically, I have an XML 'image template' stored in a string. I can store my image to media folder and insert the image ID into the XML string:
string imageNode
= _xml.Replace("##imageId##", documentMainPart.GetIdOfPart(newImage));
so now I have the correct XML as a string which I need to insert into the document.
I can find my placeholder text node which I want to replace with the new image XML
var placeholder = documentMainPart.Document.Body
.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>()
.Where(t => t.Text.Contains("##imagePlaceholder##")).First();
But this is where I get stuck. I can't see how to do a replace/insert which will take an XML string. I've managed to get my XML output as text in the document, but I beed to somehow convert it into an XML element.
If you are asking how you import the XML that displays the image then it shouldn't be a big problem.
How you store the image I'm not sure though, but I guess you will have to import it with a proper name somewhere inside the .docx but I'm assuming you know this by reading your post.
Replacing the placeholder with the image xml thingy is easy
var parent = placeholder.Parent;
parent.ReplaceChild(imageXML, placeholder);
Here you are actually replacing the image thingy with the text tag but I can't be sure how that would work. I know that a Image could be within a run tag wich I assume is the parent of your text tag.
Now if your XML you get form your command is correct you should be OK. It should be Drawing/Inline/Graphic root I think.
Please comment If I'm misunderstanding your question
To convert your string representation to an xml node belonging to the xml document, use XmlDocument.CreateFragment:
XmlDocumentFragment docFrag = doc.CreateDocumentFragment();
docFrag.InnerXml = imageXML;
placeholder.Parent.ReplaceChild(docFrag,placeholder);