.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Windows Application >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Show XML File in RichTextBox.

Posted By:Lalij Mer       Posted Date: February 19, 2010    Points: 25    Category: Windows Application    URL: http://www.dotnetspark.com  

Show XML File in RichTextBox. In this tutorial, we will explore how we can use LINQ to add data to an XML form, and then how we can extract this data and display it on our form.
 

WinForms are the Windows Applications we can create in Visual Studio. In this tutorial, we will explore how we can use LINQ to add data to an XML form, and then how we can extract this data and display it on our form.

The first thing that we will do is design our form. We will have three textboxes, two buttons and a Rich Text Box. We will use the textboxes to input data to be added to the XML file, and then one button to submit the data to be added and the other button to display the XML file in the RichTextBox.

private void button1_Click(object sender, EventArgs e)
{
XElement xml = new XElement("Persons",
new XElement("Person",
new XElement("Name", txtName.Text),
new XElement("City", txtCity.Text),
new XElement("Age", txtAge.Text)
)
);

xml.Save("XMLFile.xml");
}

This block of code creates a new XML structure for the textbox data. In this example, we have Name, City and Age. The XML output of this code will look something like this:


 
 
abc
xyz
42



The other button will display the XML file once we have added to it. For this button, we can use the following code:

private void button2_Click(object sender, EventArgs e)
{
XDocument xmlDoc = XDocument.Load("XMLFile.xml");

var persons = from person in xmlDoc.Descendants("Person")
select new
{
Name = person.Element("Name").Value,
City = person.Element("City").Value,
Age = person.Element("Age").Value,
};

richTextBox1.Text = "";
foreach (var person in persons)
{
richTextBox1.Text = richTextBox1.Text + "Name: " + person.Name + "\n";
richTextBox1.Text = richTextBox1.Text + "City: " + person.City + "\n";
richTextBox1.Text = richTextBox1.Text + "Age: " + person.Age + "\n\n";
}

if (richTextBox1.Text == "")
richTextBox1.Text = "No Results.";
} 

This method loads the XML file and then checks for the Name, City and Age elements. It returns all the data in the XML file and outputs it to the RichTextBox. The entire code-behind for this application is as follows:


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.Xml.Linq;

namespace WinForm_XML_Adding_cs
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button2_Click(object sender, EventArgs e)
{
XDocument xmlDoc = XDocument.Load("XMLFile.xml");

var persons = from person in xmlDoc.Descendants("Person")
select new
{
Name = person.Element("Name").Value,
City = person.Element("City").Value,
Age = person.Element("Age").Value,
};

richTextBox1.Text = "";
foreach (var person in persons)
{
richTextBox1.Text = richTextBox1.Text + "Name: " + person.Name + "\n";
richTextBox1.Text = richTextBox1.Text + "City: " + person.City + "\n";
richTextBox1.Text = richTextBox1.Text + "Age: " + person.Age + "\n\n";
}

if (richTextBox1.Text == "")
richTextBox1.Text = "No Results.";
}

private void button1_Click(object sender, EventArgs e)
{
XElement xml = new XElement("Persons",
new XElement("Person",
new XElement("Name", txtName.Text),
new XElement("City", txtCity.Text),
new XElement("Age", txtAge.Text)
)
);

xml.Save("XMLFile.xml");
}
} 
} 

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend