0

I attempting to display some XML results based on a user submitted xml document and user submitted XPath syntax into a rich text box. My issue is a I keep getting the error:

  An unhandled exception of type 'System.NullReferenceException' occurred in WindowsFormsApplication1.exe
  Additional information: Object reference not set to an instance of an object.

Here is my code below.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Collections.Generic;
using System.ComponentModel;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = "XML |*.xml";
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(ofd.FileName); 
            richTextBox1.Text = xDoc.SelectSingleNode(textBox1.Text).InnerText;
        }
    }
}

}

A.Lawson
  • 1
  • 2

1 Answers1

1

Change this line

richTextBox1.Text = xDoc.SelectSingleNode(textBox1.Text).InnerText;

to

XmlNode node = xDoc.SelectSingleNode(textBox1.Text);
if(node != null)
   richTextBox1.Text = node.InnerText;

And investigate why node is null.

Krsh
  • 11
  • 2