My Goal: I want to parse a XML file for Catalog Number 987654 and display it in a textbox. My XML file looks like this:
<?xml version="1.0"?>
<!-- This file was generated by the installer. -->
<ModificationMap>
<replace>
<replace>
<replace>
<!-- Changed Serial Number and Product ID -->
<text symbol="__EXAMPLE_SERIALNUMBER__">123456789</text>
<text symbol="__EXAMPLE_CATALOGNUMBER__">987654</text>
<text symbol="__MY_XMLPROGRAM__">300</text>
<text symbol="__REGISTRATION_EXAMPLE__">20</text>
<text symbol="__REGISTRATION_EXAMPLEVERSION__">20</text>
<!-- Asset Profile -->
<text symbol="__ASSET_MEMBERNAME__">MY_PROGRAM</text>
<text symbol="__ASSET_FRIENDLYNAME__">XMLFile</text>
<text symbol="__ASSET_DESCRIPTION__">XMLFile</text>
</replace>
<replace>
<delete>
<replace>
<replace>
</ModificationMap>
Code:
public Form1()
{
InitializeComponent();
var dict = XDocument.Load(@"C:\Users\Smith\Desktop\example.xml")
.Descendants("text")
.ToDictionary(f => f.Attribute("__EXAMPLE_CATALOGNUMBER__").Value,
f => f.Attribute("symbol").Value);
textBox1.Text = dict["__EXAMPLE_CATALOGNUMBER__"];
}
I am getting an error telling me:
NullReferenceException was unhandled.
I think this approach to my goal is wrong. I am new to C# coding.