TechyMedia

A .NET Technology blog | .NET,C# Progamming,C# tutorials,WPF WCF|Software Engineer

NAVIGATION - SEARCH

Read XML File in C# - Read Content From XML File

Example for the XMLTextReader class to read the xml element names. Read XML Content From a File- C#.

The example below explains the XMLTextReader class to read the xml element names and content text data from an xml file in to a string.I haven't taken care of the various exception possibilities as explained in the above code.Do it as per your need.

C++ Ultimate Video Training - Best Course Ever icon 

 

Code: Read XML Content From a File- C#

 

First add using System.Xml namespace to the calss.

 

using (XmlTextReader xmlTextReader = new XmlTextReader("D:\\Sample.xml"))
{
                                string xmlData = string.Empty;
        while (xmlTextReader.Read())
        {
        reader.MoveToContent();
        if (xmlTextReader.NodeType == XmlNodeType.Element)
            xmlData += "<"+ xmlTextReader.Name + ">\n";
        if (xmlTextReader.NodeType == XmlNodeType.Text)
            xmlData += xmlTextReader.Value + "\n";
        }
}

 

//Display the xml content in windowsform using messagebox.show

MessageBox.Show(xmlData);

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading