I'm developing my first WPF application with C# but I have a problem when I'm trying to read a Xml attribute.
I have the following Xml:
<?xml version="1.0" encoding="utf-8"?>
<Dictionary EnglishName="Italian" CultureName="Italian" Culture="">
<!-- MainWindow -->
<Value ID="WpfApplication1.MainWindow.BtnDrawCircle" Content="Circonferenza"/>
<Value ID="WpfApplication1.MainWindow.BtnDrawLine" Content="Linea"/>
....
....
</Dictionary>`
Now I try to get the Attribute "Content" with the following method:
public static string ReadNodeAttribute(string IDAttribute)
{
try
{
XmlDocument _Doc = new XmlDocument();
_Doc.Load("myPath\\Language\\it-IT.xml");
string _Value = _Doc.SelectSingleNode("//Value[@ID=" + IDAttribute + "]").Attributes["Content"].Value.ToString();
return _Value;
}
catch (Exception ex)
{
return null;
}
}
But it doesn't work:
Error : ex {"Object reference not set to an instance of an object."} System.Exception {System.NullReferenceException}
IDAttribute?var xn = _Doc.SelectSingleNode("//Value[@ID=" + IDAttribute + "]");and see what you get forxn. You can use the watch window in the debugger. Once you figure out the right xpath expression to get the node you want, you can look at itsAttributescollection in the debugger and see what is in there. For all I know, the relative path to your file may be wrong. Check _Doc after Load!