Getting, Reading XML Document : make it easy
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
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2. Write Code-Behind for implementation:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Reading XML Document</title>
</head>
<body>
<form id="form1" runat="server">
<div id="dvXML" runat="server" visible="false">
<asp:TextBox id="txtXML" runat="server" />
</div>
<div>
<asp:Button ID="btnXML" runat="server" Text="Read XML" onclick="btnXML_Click" />
</div>
</form>
</body>
</html>
/* 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%
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.










Leave your response!
You must be logged in to post a comment.