Use XmlReader object - How to read xml file by using XmlReader object in visual C#

Use XmlReader object – How to read xml file by using XmlReader object in visual C#

We will use XmlReader object available in Visual C#.net. XmlReader object is very fast to use. It is memory efficient to read large xml files. Using XmlReader object to read xml from a file is very simple.


Which xml we want to read from xml file <ROOT>
  <InstallationId>00000000-0000-0000-0000-000000000000</InstallationId>
  <Environment>Dev</Environment>
  <InstallationPath>D:\Users\app.exe</InstallationPath>
</ROOT>

C# Code for reading xml from xml file         string appConfigFilePath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + “\\app.config”;

        XmlReader reader = XmlReader.Create(appConfigFilePath);
        while (reader.Read())
        {
          switch (reader.NodeType)
          {
            case XmlNodeType.Element: // The node is an element.
              switch (reader.Name)
              {
                case “InstallationId”:
                  break;
                case “Environment”:
                  break;
                case “InstallationPath”:
                  break;
              }
              break;
            case XmlNodeType.Text:  // The text inside the node
              // Do some manipulation for the text
              break;
          }
        }

Leave a Comment