recently i've started learning C# and encountered a problem with my WPF application after publishing it.
First, let me explain what the application should do: Read the xml-file "testxml.xml", and write the names given there in a ListBox. The xml-File lies in \myProject\bin\Debug.
My code:
public partial class Page1 : Page
{
private string path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\testxml.xml";
List myList = new List();
public Page1()
{
InitializeComponent();
ReadXML(path, myList,ListBox1);
}
public static void ReadXML(string path, List<TestXML> myList, ListBox ListBox)
{
if (File.Exists(path))
{
XmlSerializer deserializer = new XmlSerializer(typeof(List<TestXML>));
TextReader reader = new StreamReader(path);
object obj = deserializer.Deserialize(reader);
myList = (List<TestXML>) obj;
reader.Close();
}
for (int i = 0; i < myList.Count; i++)
{
ListBox.Items.Add(myList[i].Name);
}
}
}
This works fine in debug mode. So, I tried to publish it. Therefore, I've included the xml-file in my project and changed its settings "Build Action" to "Content" and "Change to Output Directory" to "Copy Always". Furthermore, I changed its "Publish Status" from "Data File" to "Include" in my project properties. I published it via publish wizard to "C:\user\" and select "From a CD-ROM or DVD-ROM".
This gave me an xbap-File and an "Application Files"-Folder in "C:\users\". The xml-file is available under "\Application Files\MyProject_1_0_0_x\bin\Debug". But when I execute the xbap-File, it gives me a huge error that starts with
"System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation."
My suggestion so far is the directory of the xml-file, which perhaps doesn't work after publishing. What would be the correct directory? Or is there another mistake I'm not aware of?
Any help is greatly appreciated!