Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Saturday, December 13, 2008

MSXML to System.xml with C# - XSL Transformation

Good Link for XSLT Transformation

XSL Transformation


This step-by-step article describes the features of MSXML and .NET Framework Classes for XML processing. This article also includes samples on how to use MSXML in Microsoft Visual Basic 6.0 and how to use .NET Framework Classes for XML processing.

http://support.microsoft.com/kb/330589

This article mainly focus on following
  • MSXML vs. System.xml for XML Processing
  • XSL Transformation
  • XML Validation
  • XML Navigation

Wednesday, October 31, 2007

Reading XML Document in .Net

There are 3 preferable ways to read XML document.

  • XMLDocument Class - It allows to insert, update, delete or move a node.
  • XMLTextReader Class - It allows read data in fast forward-only manner.
  • XMLValidatingReader Class - It allows to validate XML document against Document Type Definition (DTD), XML Schema definition(XSD) and XML Data Reduced(XDR).

XMLTextReader and XMLValidatingReader Class is derived from XMLReader class which provides
  • fast,
  • non-cacheable,
  • read-only, and
  • forward-only access to XML data.

Choosing between XMLDocument, XMLTextReader and XMLValidatingReader Class.
  • Use XMLDocument when
    • You want to perform operation like Insert, Update and delete.
    • Memory is not a constraint
  • Use XMLTextReader when
    • You want to read data in forward-only manner.
    • When memory is constraint.
  • Use XMLValidatingReader when
    • You want to validate XML document against DTD, XSD and XDR.

What is XML DOM (Document Object Model)

Why is XML DOM?
XML is a standard by which two incompatible system can communicate. XML DOM works with XML data in your application. It is used to read, write and modify XML document programatically.


Understanding Nodes in XML Document


Node type

Description

Root

This node type is the container for all the nodes and is also known as the document root.

Element

This node type represents element nodes.

Attribute

This node type represents the attributes of an element node.

Text

This node type represents the text that belongs to a particular node or to an attribute.



Example of XML Document, employee.xml
<?xml version="1.0" encoding="utf-8" ?>
<employees>
<employee>
<FirstName>Ajay</FirstName>
<LastName>Thakur</LastName>
<DateOfBirth>08/09/1968</DateOfBirth>
<DateOfJoining>04/01/1992</DateOfJoining>
<Address>2010 Stanley Dr., Charlotte, NJ 08830</Address>
<Basic>2100</Basic>
<Designation>HR Manager</Designation>
<LeaveBalance>12</LeaveBalance>
</employee>
<employee>
<FirstName>Ganesh</FirstName>
<LastName>Patil</LastName>
<DateOfBirth>01/12/1972</DateOfBirth>
<DateOfJoining>06/01/2000</DateOfJoining>
<Address>7862 Freepoint Pkwy, Tampa, NJ 08820</Address>
<Basic>1400</Basic>
<Designation>Software Professional</Designation>
<LeaveBalance>4</LeaveBalance>
</employee>
</employees>


Understanding XML Document
  • Every XML Document contains a single root element that contains all other nodes of the XML Document. Here <employees> is a root element.
  • XML Element is a Record which contains information. Here <employee>
  • XML Element Attribute further describe the Record details. Here <FirstName> etc are XML Element Attributes.

What is XML Document Object?
XMLDocument provides an in-memory representation of and XML document.

Example of XML Document
Create a Asp.net Website
Create a XML Document and name it employee.xml

Loading of XML Document
You can load xml document using XMLDocument object's load method

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
XmlDocument EmpXMLDoc = new XmlDocument();
string XMLDocPath = Server.MapPath("Employee.xml");
EmpXMLDoc.Load(XMLDocPath);
Response.Write(EmpXMLDoc.InnerText.ToString());
}
}


Saving XML Document
You can save XML Document with XMLDocument.Save method
Example: XMLDocument.Save("Employee.xml");


Read XML Document and display on WebPage
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
XmlDocument EmpXMLDoc = new XmlDocument();
string XMLDocPath = Server.MapPath("Employee.xml");
EmpXMLDoc.Load(XMLDocPath);

//Read XML Document //Note: Begin for ChildNode[1]
foreach (XmlNode node1 in EmpXMLDoc.ChildNodes[1])
{
foreach (XmlNode node2 in node1.ChildNodes)
{
Response.Write("<b>" + node2.Name + "</b>: " + node2.InnerText + " ");
}
Response.Write("<br>");
}
}
}

//Output

Most Recent Post

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape