XML FILE Opening and Reading using C#.NET or ASP.NET
In the previous example we have created the XML file using C#.NET. Now we are going to read that XML file based on required tag. In the below program we are giving XML file as input and we will open XML file. We will read the XML node values from the XML file and showing on the screen.Input to the program.
Output from the program:
Program:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.IO; namespace XMLFILEREADING { class Program { static void Main(string[] args) { //Declarations string str = null; string filePath = "c://customer.xml"; XmlDataDocument xmlDocument = new XmlDataDocument(); XmlNodeList xmlNodeListData; //open xml file using IO operation filestream. FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); //loading xml document xmlDocument.Load(fs); //getting xml tag data. xmlNodeListData = xmlDocument.GetElementsByTagName("Customer"); //looping all internal nodes of the customer tag for (int iIndex = 0; iIndex <= xmlNodeListData.Count - 1; iIndex++) { str = xmlNodeListData[iIndex].ChildNodes.Item(0).InnerText.Trim() + " ---> " + xmlNodeListData[iIndex].ChildNodes.Item(1).InnerText.Trim(); Console.WriteLine(str); } Console.ReadLine(); } } }
See more OOPS DOTNET Interview Questions
See more ASP.NET, C#.NET, SQL Server and more Interview Questions
Previous Home Next
Post a Comment
Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.