Part 2 : Reading Xml in C#.Net : XmlDocument

Namespace :  System.xml
Class name :XmlDocument()
Description 
This class implements the W3C Document Object Model (DOM) Level 1 Core and the Core DOM Level 2. The DOM is an in-memory (cache) tree representation of an XML document and enables the navigation and editing of this document. Because XmlDocument implements the IXPathNavigable interface it can also be used as the source document for the XslTransform class.
The XmlDataDocument class extends XmlDocument and allows structured data to be stored, retrieved, and manipulated through a relational DataSet. This class allows components to mix XML and relational views of the underlying data.
Coding :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;//Xml Class
using System.Windows.Forms;
using System.IO;
namespace Xml_Sample
{
public partial class frmRead : Form
{
public frmRead()
{
InitializeComponent();
}

private void toolStripButton1_Click(object sender, EventArgs e)
{
op.ShowDialog();//open file dialog
this.toolStripTextBox1.TextBox.Text = op.FileName;
//load the xml file
if (this.toolStripTextBox1.TextBox.Text != string.Empty)
//read xml file
Read_Xml(this.toolStripTextBox1.TextBox.Text.Trim());
else
MessageBox.Show("Select Xml file"); 
}


public void Read_Xml(string filepath)
{
try{
//create new instance of class
XmlDocument xdoc = new XmlDocument();
xdoc.Load(filepath);//Loads xml file



//format the xml to show in richtextbox
string output=formatDocument(xdoc); 
 //Shows the xml 
this.richTextBox1.Text = output;
}

//If error Occured
catch (Exception ex)
{
//Show error message
MessageBox.Show(ex.Message, "Status Error",MessageBoxButtons.OK, MessageBoxIcon.Error); 

}

}

private string formatDocument(XmlDocument xdoc)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
XmlTextWriter xtxtWriter = new XmlTextWriter(sw);
xtxtWriter.Formatting = Formatting.Indented;
xdoc.WriteTo(xtxtWriter);
return(sb.ToString());
} 
}
}
Xml_Sample.rar 

posted under , , , | 0 Comments

Part 1 : Introduction to Xml Classes in C#.Net

Defination : Xml 

XML, or EXtensible Markup Language, is a platform-independent way to represent data. Simply put, XML enables you to create data that can read by any application on any platform. You can even edit and create it by hand, because it is based on the same tag-based technology that underlies HTML

An Example

XML Element 

An XML element is everything from (including) the element's start tag to (including) the element's end tag.
An element can contain:
  • other elements
  • text
  • attributes
  • or a mix of all of the above..   
XML Attributes 

In HTML, attributes provide additional information about elements.Attributes often provide information that is not a part of the data. In the example below, the file type is irrelevant to the data, but can be important to the software that wants to manipulate the element.

Xpath

XPath is used to navigate through elements and attributes in an XML document.
XPath is a major element in W3C's XSLT standard - and XQuery and XPointer are both built on XPath expressions. Read More



posted under , , | 1 Comments
Newer Posts Older Posts Home

We have seen by

    Followers

    Translate


Recent Comments