| Next Tip?
Home » Asp.net, CSharp

Getting, Reading XML Document : make it easy

28 October 2008 259 views No Comment
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

To generate XML-Schema – it easy with the use of <b>xmlTextReader</b>.
Here, I am try to retrieve xmlSchema with following code-snippet:

1. Create a Design Page

&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

2. Write Code-Behind for implementation:

&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head runat="server"&gt;
&lt;title&gt;Reading XML Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form id="form1" runat="server"&gt;
&lt;div id="dvXML" runat="server" visible="false"&gt;
&lt;asp:TextBox id="txtXML" runat="server" /&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;asp:Button ID="btnXML" runat="server" Text="Read XML" onclick="btnXML_Click" /&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

/* This Example is a part of different
* examples shown in Book:
* C#2005 Beginners: A Step Ahead
* Written by: Gaurav Arora
* Reach at : Gaurav Arora*/

#region Using Namespaces
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Xml.Linq;
#endregion

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
private void ReadXML()
{
XmlTextReader xmlReader = new XmlTextReader("http://www.msdotnetheaven.com/?feed=rss2");
//XmlTextReader xmlReader = new XmlTextReader("/myXML.xml");

xmlReader.Read();

while (xmlReader.Read())
{

// Move to fist element

xmlReader.MoveToElement();

txtXML.Text = ("Name:" + xmlReader.Name);

txtXML.Text += ("Base URI:" + xmlReader.BaseURI);

txtXML.Text += ("Local Name:" + xmlReader.LocalName);

txtXML.Text += ("Attribute Count:" + xmlReader.AttributeCount.ToString());

txtXML.Text += ("Depth:" + xmlReader.Depth.ToString());

txtXML.Text += ("Line Number:" + xmlReader.LineNumber.ToString());

txtXML.Text += ("Node Type:" + xmlReader.NodeType.ToString());

txtXML.Text += ("Attribute Count:" + xmlReader.Value.ToString());
}
}

protected void btnXML_Click(object sender, EventArgs e)
{
//Call the readxml
ReadXML();

//Set visible to TextBox
dvXML.Visible = true;

}
}

Following are the step(s):
1. Start Visual Studio
2. Create a new project
3. Add above two files
4. Press F5
5. Click on ReadXML

Popularity: 1%

Post to Twitter Tweet This Post

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Ads by Lake Quincy Media

Leave your response!

You must be logged in to post a comment.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes