Parse XML File for a number and display in textbox - c#

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"];

Related

Displaying one item from an xml file and dispalying it c#

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.

How do I use Linq-to-XML on the contents of a string (not document)?

Hi and thanks for looking!
Background
I am working on a developer tool for our dev team that parses content from MS Word into a Windows form with text boxes. We do some processing on the text, then submit the form to a database.
Some of the textboxes in the form contain Word XML which we need to clean up and convert to our own XML to later use with XSLT.
When the form populates, I would like to take the Word XML and use Linq to search for certain tags (example: <w:t>SOME TEXT</w:t>) and convert it to our own XML (<Text>SOME TEXT</Text>) before it gets to the textbox.
Question
How do I use Linq-to-Xml on the contents retrieved from a string in the pre-processing stage? I know how to instantiate an XDocument, but this is just a string so I am stumped. Probably missing something simple.
Thanks!
You can use the XDocument.Parse Method to create an XDocument from a string.

How do I parse a word between two elements?and extract it to database

let me brief there is a file (it might be anything sgml,xml,sgml etc).i will give that file as input and read the file using c#..so the job is now that i know that there are certain tags where in between this data is present...so the point is i have to scan that line by line like pattern matching and extract the data which is present in between tags ,...so please help me getting this ...
I think if file is some how in xml format you can use XPath to traverse/manipulate its elements..
http://support.microsoft.com/kb/308333
http://www.codeproject.com/KB/cpp/myXPath.aspx
Regards.

Insert an XML string into an openXML document

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);

using linq for this:

i have just started learning linq because i like the sound of it. and so far i think im doing okay at it.
i was wondering if Linq could be used to find the following information in a file, like a group at a time or something:
Control
Text
Location
Color
Font
Control Size
example:
Label
"this is text that will
appear on a label control at runtime"
23, 77
-93006781
Tahoma, 9.0, Bold
240, 75
The above info will be in a plain file and wil have more than one type of control and many different sizes, font properties etc associated with each control listed. is it possible in Linq to parse the info in this txt file and then convert it to an actual control?
i've done this using a regex but regex is too much of a hassle to update/maintain.
thanks heaps
jase
Edit:
Since XML is for structured data, would Linq To XML be appropriate for this task? And would you please share with me any helpful/useful links that you may have? (Other than MSDN, because I am looking at that now. :))
Thank you all
If you are generating this data yourself, then I HIGHLY recommend you store this in an XML file. Then you can use XElement to parse this.
EDIT: This is exactly the type of thing that XML is designed for, structured data.
EDIT EDIT: In response to the second question, Linq to XML is exactly what your looking for:
For an example, here is a couple of links to code I have written that parses XML using XElements. It also creates a XML document.
Example 1 - Loading and Saving: have a look under the FromXML() and ToXML() methods.
Example 2 - Parsing a large XML doc: have a look under the ParseXml method.
Hope these get you going :D
LINQ is good for filtering off rows, selecting relevant columns etc.
Even if you use LINQ for this, you will still need regex to select the relevant text and do the parsing.

Categories