Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
Pure VPN - Up to 27 Months
vgp
Updated:
Friday at 8:10 AM
එක පැකේජ් එකයි මාසෙටම Unlimited Internet. තාමත් DATA CARD දාන්න සල්ලි වියදම් කරනවද? අඩුම මිලට අපෙන්.
sayuru bandara
Updated:
Jun 2, 2026
Ad icon
ඉන්ටර්නෙට් එකෙන් හරියටම සල්ලි හොයන්න සහ Success වෙන්න කැමතිද? 🚀 (E-Money & Success Stories)
siri sumana
Updated:
May 30, 2026
Gemini AI PRO 18 months Offer
Hawaka
Updated:
May 27, 2026
Ad icon
koko account
DasunEranga
Updated:
May 27, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
Education
Read XML file from c# using Microsoft visual studio 2008
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="ra20002" data-source="post: 8296408" data-attributes="member: 110775"><p>Topic says what i'm going to teach you.But first you need to know some theories about XML and C#.</p><p>System.Xml.XmlDocument class provides handling XML </p><p>There are two main .NET APIs for working with XML</p><p>XML document object model (DOM) - XmlReader and XmlWriter</p><p>Tree Based XML handling</p><p>XML data loaded into memory.</p><p>Can search for any node.</p><p>What is XML DOM ?</p><p>It provide standard mechanism for programitically working with data.</p><p>Read XML data</p><p>XML data is load into XmlDocument object</p><p>So first we have to create a XmlDocument object .</p><p></p><p>XML data look like this...</p><p><bookstore></p><p> <book category="CHILDREN"></p><p> <title>Harry Potter</title></p><p> <author>J K. Rowling</author></p><p> <year>2005</year></p><p> <price>29.99</price></p><p> </book></p><p> <book category="WEB"></p><p> <title>Learning XML</title></p><p> <author>Erik T. Ray</author></p><p> <year>2003</year></p><p> <price>39.95</price></p><p> </book></p><p></bookstore></p><p></p><p>Contains root node and multiple child nodes (root node - bookstore)</p><p>Each node has the parent node (Except root node)</p><p>Each node can have siblings (Except root node).</p><p></p><p>So if you want to learn XML deeply just go to w3schools.com. There are lot of tutorials available.</p><p></p><p>OK lets do this. Now we are going to make a new project in Microsoft visual studio 2008 to read a simple XML file.</p><p>First you need to launch Visual studio 2008 and Select new Project like this...</p><p><img src="http://2.bp.blogspot.com/_vEsc0m17Dec/TJSk7cmyyeI/AAAAAAAAAAM/FZMoMqmT-sE/s640/1.jpg" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p>After that select Visual C# Windows Forms Application and Give a name to the project and save where ever you want...</p><p><img src="http://3.bp.blogspot.com/_vEsc0m17Dec/TJSlk5YVAbI/AAAAAAAAAAU/XbEzdxCSweU/s640/2.jpg" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p></p><p>Right Click the project and Select Add --> New Folder . Rename that folder into XML.</p><p><img src="http://2.bp.blogspot.com/_vEsc0m17Dec/TJSmINzjnGI/AAAAAAAAAAc/7fZvWeN2gPo/s640/3.jpg" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p>Right Click the XML folder that you create and Select Add -> New Item . Select XML file and name it as BookStore.xml and Click Add.</p><p><img src="http://1.bp.blogspot.com/_vEsc0m17Dec/TJSmt0T_d-I/AAAAAAAAAAk/hzaEUeXmIJ8/s640/4.jpg" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p></p><p>Copy and Paste this XML code into BookStore.xml file.</p><p></p><p>[CODE]<?xml version="1.0" encoding="utf-8" ?></p><p><bookstore></p><p> <book publicationdate="2000" ISBN="212"></p><p> <title>C#</title></p><p> <author>Author 1</author></p><p> <price>5000</price></p><p> </book></p><p> <book publicationdate="2009" ISBN="312"></p><p> <title>Java for dummies</title></p><p> <author>Author 2</author></p><p> <price>12000</price></p><p> </book></p><p> <book publicationdate="2008" ISBN="412"></p><p> <title>ASP.NET</title></p><p> <author>Author 3</author></p><p> <price>6000</price></p><p> </book></p><p></bookstore></p><p>[/CODE]</p><p> Now right click the Form1.cs and select view design . Create design like this and rename button name as btnLoad and Text as Load Data.</p><p>Also drag and drop a textbox and click the small arrow in the right upper corner and tick the multiline. Change the name as txtresult.</p><p></p><p><img src="http://4.bp.blogspot.com/_vEsc0m17Dec/TJSoGr0Dt3I/AAAAAAAAAAs/zirUBGgdTfc/s640/6.jpg" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p></p><p>Double click the button and it will automatically direct to the code.</p><p>First you need 2 references and a varialble....</p><p></p><p>using System.Xml;</p><p>using System.IO;</p><p></p><p> private string _xmlFile;</p><p></p><p>After that go to the design view and double click the Form1.cs and it will automatically go to code and create Form1_Load event.In the Form1_Load paste this code..</p><p>[CODE]if (!SetxmlFilePath())</p><p> {</p><p> MessageBox.Show("Unable to load XML file.");</p><p> } </p><p>[/CODE]</p><p>Now create the private bool SetxmlFilePath() method and paste this code inside that method..</p><p>[CODE]FileInfo fi = new FileInfo(Application.StartupPath + @"\..\..\XML\BookStore.xml");</p><p> if (fi.Exists)</p><p> {</p><p> _xmlFile = fi.FullName;</p><p> }</p><p> else</p><p> {</p><p> return false;</p><p> }</p><p> return true;</p><p>[/CODE]</p><p> Now goto design area and click the Load button and you will get the btnLoad_click. Inside that paste this code..</p><p>[CODE]DumpContents(_xmlFile);[/CODE]</p><p>Now Create private void DumpContents(string _xmlFile) mathod and paste this code inside that method..</p><p> [CODE]string publishDate = null;</p><p> string title = null;</p><p> string author = null;</p><p> string ISBN = null;</p><p> string price = null;</p><p> XmlElement element = null;</p><p></p><p> XmlDocument doc = new XmlDocument();</p><p> doc.Load(_xmlFile);</p><p></p><p> foreach (System.Xml.XmlNode node in doc.SelectNodes("//book"))</p><p> {</p><p> title = GetNodeValue(node, "title");</p><p> author = GetNodeValue(node, "author");</p><p> price = GetNodeValue(node, "price");</p><p></p><p> element = (XmlElement)node;</p><p> publishDate = element.GetAttribute("publicationdate");</p><p> ISBN = element.GetAttribute("ISBN");</p><p></p><p> DisplayBook(title, author, publishDate, ISBN, price);</p><p> }[/CODE]</p><p>Create this method..</p><p>[CODE]private void DisplayBook(string title, string author, string publishDate, string ISBN, string price)</p><p> {</p><p> string results = string.Format("{0} ({1}) by {2} (ISBN: {3}), ${4}",title, publishDate, author,</p><p> ISBN, price);</p><p> AddText(results);</p><p> }</p><p>[/CODE]</p><p>After that create AddText methos like this..</p><p>[CODE]private void AddText(string Text)</p><p> {</p><p> txtResult.AppendText(Text + Environment.NewLine);</p><p> }</p><p>[/CODE]At last create this method to get the node value</p><p> [CODE]private string GetNodeValue(XmlNode parentNode, string nodeName)</p><p> {</p><p> string retval = null;</p><p></p><p> XmlNode node =parentNode.SelectSingleNode(nodeName);</p><p> if (node != null)</p><p> {</p><p> retval = node.InnerText;</p><p> }</p><p> return retval;</p><p> }[/CODE]</p><p> If you do it correctly right click the project and select build . If there any compilation error you have to fix it to get the result.</p><p>If there are no errors click the debug button , after the form shows click the Load Data button and the result textbox shows the xml file.</p><p><img src="http://3.bp.blogspot.com/_vEsc0m17Dec/TJSuOOjo0nI/AAAAAAAAAA0/Euaz4dS6aG0/s640/last.jpg" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p></p><p></p><p>You can download the sample project</p><p><a href="http://hotfile.com/dl/70230733/5270b8d/ReadXML.rar.html" target="_blank">http://hotfile.com/dl/70230733/5270b8d/ReadXML.rar.html</a></p><p></p><p><a href="http://dummiestutorials.blogspot.com/2010/09/read-xml-file-from-c-using-microsoft.html" target="_blank">source</a></p></blockquote><p></p>
[QUOTE="ra20002, post: 8296408, member: 110775"] Topic says what i'm going to teach you.But first you need to know some theories about XML and C#. System.Xml.XmlDocument class provides handling XML There are two main .NET APIs for working with XML XML document object model (DOM) - XmlReader and XmlWriter Tree Based XML handling XML data loaded into memory. Can search for any node. What is XML DOM ? It provide standard mechanism for programitically working with data. Read XML data XML data is load into XmlDocument object So first we have to create a XmlDocument object . XML data look like this... <bookstore> <book category="CHILDREN"> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"> <title>Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore> Contains root node and multiple child nodes (root node - bookstore) Each node has the parent node (Except root node) Each node can have siblings (Except root node). So if you want to learn XML deeply just go to w3schools.com. There are lot of tutorials available. OK lets do this. Now we are going to make a new project in Microsoft visual studio 2008 to read a simple XML file. First you need to launch Visual studio 2008 and Select new Project like this... [IMG]http://2.bp.blogspot.com/_vEsc0m17Dec/TJSk7cmyyeI/AAAAAAAAAAM/FZMoMqmT-sE/s640/1.jpg[/IMG] After that select Visual C# Windows Forms Application and Give a name to the project and save where ever you want... [IMG]http://3.bp.blogspot.com/_vEsc0m17Dec/TJSlk5YVAbI/AAAAAAAAAAU/XbEzdxCSweU/s640/2.jpg[/IMG] Right Click the project and Select Add --> New Folder . Rename that folder into XML. [IMG]http://2.bp.blogspot.com/_vEsc0m17Dec/TJSmINzjnGI/AAAAAAAAAAc/7fZvWeN2gPo/s640/3.jpg[/IMG] Right Click the XML folder that you create and Select Add -> New Item . Select XML file and name it as BookStore.xml and Click Add. [IMG]http://1.bp.blogspot.com/_vEsc0m17Dec/TJSmt0T_d-I/AAAAAAAAAAk/hzaEUeXmIJ8/s640/4.jpg[/IMG] Copy and Paste this XML code into BookStore.xml file. [CODE]<?xml version="1.0" encoding="utf-8" ?> <bookstore> <book publicationdate="2000" ISBN="212"> <title>C#</title> <author>Author 1</author> <price>5000</price> </book> <book publicationdate="2009" ISBN="312"> <title>Java for dummies</title> <author>Author 2</author> <price>12000</price> </book> <book publicationdate="2008" ISBN="412"> <title>ASP.NET</title> <author>Author 3</author> <price>6000</price> </book> </bookstore> [/CODE] Now right click the Form1.cs and select view design . Create design like this and rename button name as btnLoad and Text as Load Data. Also drag and drop a textbox and click the small arrow in the right upper corner and tick the multiline. Change the name as txtresult. [IMG]http://4.bp.blogspot.com/_vEsc0m17Dec/TJSoGr0Dt3I/AAAAAAAAAAs/zirUBGgdTfc/s640/6.jpg[/IMG] Double click the button and it will automatically direct to the code. First you need 2 references and a varialble.... using System.Xml; using System.IO; private string _xmlFile; After that go to the design view and double click the Form1.cs and it will automatically go to code and create Form1_Load event.In the Form1_Load paste this code.. [CODE]if (!SetxmlFilePath()) { MessageBox.Show("Unable to load XML file."); } [/CODE] Now create the private bool SetxmlFilePath() method and paste this code inside that method.. [CODE]FileInfo fi = new FileInfo(Application.StartupPath + @"\..\..\XML\BookStore.xml"); if (fi.Exists) { _xmlFile = fi.FullName; } else { return false; } return true; [/CODE] Now goto design area and click the Load button and you will get the btnLoad_click. Inside that paste this code.. [CODE]DumpContents(_xmlFile);[/CODE] Now Create private void DumpContents(string _xmlFile) mathod and paste this code inside that method.. [CODE]string publishDate = null; string title = null; string author = null; string ISBN = null; string price = null; XmlElement element = null; XmlDocument doc = new XmlDocument(); doc.Load(_xmlFile); foreach (System.Xml.XmlNode node in doc.SelectNodes("//book")) { title = GetNodeValue(node, "title"); author = GetNodeValue(node, "author"); price = GetNodeValue(node, "price"); element = (XmlElement)node; publishDate = element.GetAttribute("publicationdate"); ISBN = element.GetAttribute("ISBN"); DisplayBook(title, author, publishDate, ISBN, price); }[/CODE] Create this method.. [CODE]private void DisplayBook(string title, string author, string publishDate, string ISBN, string price) { string results = string.Format("{0} ({1}) by {2} (ISBN: {3}), ${4}",title, publishDate, author, ISBN, price); AddText(results); } [/CODE] After that create AddText methos like this.. [CODE]private void AddText(string Text) { txtResult.AppendText(Text + Environment.NewLine); } [/CODE]At last create this method to get the node value [CODE]private string GetNodeValue(XmlNode parentNode, string nodeName) { string retval = null; XmlNode node =parentNode.SelectSingleNode(nodeName); if (node != null) { retval = node.InnerText; } return retval; }[/CODE] If you do it correctly right click the project and select build . If there any compilation error you have to fix it to get the result. If there are no errors click the debug button , after the form shows click the Load Data button and the result textbox shows the xml file. [IMG]http://3.bp.blogspot.com/_vEsc0m17Dec/TJSuOOjo0nI/AAAAAAAAAA0/Euaz4dS6aG0/s640/last.jpg[/IMG] You can download the sample project [URL="http://hotfile.com/dl/70230733/5270b8d/ReadXML.rar.html"]http://hotfile.com/dl/70230733/5270b8d/ReadXML.rar.html[/URL] [URL="http://dummiestutorials.blogspot.com/2010/09/read-xml-file-from-c-using-microsoft.html"]source[/URL] [/QUOTE]
Insert quotes…
Verification
Winadiyakata thappara keeyak tibeda?
Post reply
Top
Bottom