Click here to Skip to main content
14,545,215 members
Rate this:
Please Sign up or sign in to vote.
I'm using Windows Application in WPF C# Lang in Embedded Field. and Console window is only for Print Statements. Writting a Program a Load XML value to Listbox and SAVE Listbox items to XML flie. Listbox take items from TextBox. when Add button click textbox item added to Listbox. when i click SAVE Button 1st Listbox item will saved in xml File and then item will stored in Board [IOT related Application]

now my problem is : when im click on SAVE Button data is saved in XML file but not saved in Board, im getting some exception and application will close Automatically. Getting Exception Like This :
at System.IO.Filestream.Init(String Path, FileMode mode, FileAccess Access, Int32 rights, boolean useRights, FileStream Share, int32 buffersize, fileoption option, SECURITY_ATTRIBUTES secattrs, string msgPath, boolean bFfromProxy, boolean uselongpath, boolean checkhost)
at System.IO.FileStream..cattr(StringPath, FileMode, fileacces access, FileShare share, Int32 bufferSize)
at System.XML.xmldownloadmanager.getstream(Uri uri,ICredentials credentials, IWebProxy Proxy, REquestCachePolicy Cachepoliy)at System.XML.xmlURLResolver.getentity(Uri absoluteuri, string role, type ofobjectToReturn)
at System.Xml.xmltextReaderImpl.FinishInItUriString()
at System.Xml.xmlREaderSettings.CreateReader(String input uri, XmlParserContext Inputcontext)
at System.Xml.Linq.xElement.Load(String uri.LoadOption LoadOption)at MainWindow.Button_Click(object sender, Routed EventArgs e)
at System.Windows.EventRoute.InvokehandlerImpl(Objext Source, RoutedEventArgs Args, Boolean reRaise)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject Sender, RoutedEventArgs Args)
at System.Windows.Control.Premitives.buttonbase.OnClick()at System.Windows.Control.Button.OnClick()
at System.Windows.Control.Primitives.ButtonBase. OnmouseLeftButtonUp(MouseButtonEventArgs E)
at System.Windows.RoutedEventArgs.InvokeHandlers(Deleget Handler, ObjectTarget)
at System.Windows.EventRoute.InvokeHandlerImpl(Object Source, RoutedEventArgs Args, Boolean ReRaised)
at System.Windows.UIElement.ReRaiseEventAs(DependencyObject Sender, RoutedEventArgs Args, RoutedEvent NewEvent)
at System.Windows.UIElement.OnMouseUPThunk(Object Sender, MouseButtonEventArgs e)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegete Handler, Object Target)
at System.Windows.RoutedEventHandlerInfo invokeHandler(Object target, RoutedEventArgs RoutedEventargs)
at System.Windows.EventRoute.InvokeHandlerImpl(Object Source, RoutedEventArgs Args, Boolean Reraised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject Sender, RoutedEventArgs Args)
at System.Windows.UIElement.RaiseTrustedEvent (RoutedEventArgs Args)
at System.Windows.Input.InputManager.ProcessStageingArea()
at System.Windows.Input.InputManager.ProcessInput (InoutEventArgs Input)
at System.Windows.Input.InputProviderSite.ReportInput (InputReport inputreport)


What I have tried:

My Code is :
private void LoadXML_Click(object sender, RoutedEventArgs e)
{
    XElement element = XElement.Load("WH_Num.xml");
    foreach (XElement item in element.Elements("Number1"))
        ListBox_PhoneNumber.Items.Add(item.Value);
    foreach (XElement item in element.Elements("Number2"))
        ListBox_PhoneNumber.Items.Add(item.Value);
    foreach (XElement item in element.Elements("Number3"))
        ListBox_PhoneNumber.Items.Add(item.Value);
    foreach (XElement item in element.Elements("Number4"))
        ListBox_PhoneNumber.Items.Add(item.Value);
    foreach (XElement item in element.Elements("Number5"))
      
    MessageBox.Show("Number Loaded to XML");
}

private void SAVEXML_Click(object sender, RoutedEventArgs e)
{
    try
    {
        information info = new information();
        if (ListBox_PhoneNumber.Items.Count > 0) info.Number1 = ListBox_PhoneNumber.Items[0].ToString();
        if (ListBox_PhoneNumber.Items.Count > 1) info.Number2 = ListBox_PhoneNumber.Items[1].ToString();
        if (ListBox_PhoneNumber.Items.Count > 2) info.Number3 = ListBox_PhoneNumber.Items[2].ToString();
        if (ListBox_PhoneNumber.Items.Count > 3) info.Number4 = ListBox_PhoneNumber.Items[3].ToString();
        if (ListBox_PhoneNumber.Items.Count > 4) info.Number5 = ListBox_PhoneNumber.Items[4].ToString();
      

        SaveXML.savedata(info, "WH_Num.xml");
        MessageBox.Show("Data Saved");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

// THIS CODE IS FOR SAVE DATA IN BOARD LEVEL
    string Number1 = string.Empty;
    string Number2 = string.Empty; 
    string Number3 = string.Empty; 
    string Number4 = string.Empty; 
    string Number5 = string.Empty; 
    if (ListBox_PhnNumbr.Items.Count > 0) Number1 = ListBox_PhnNumbr.Items[0].ToString();
    if (ListBox_PhnNumbr.Items.Count > 1) Number2 = ListBox_PhnNumbr.Items[1].ToString();
    if (ListBox_PhnNumbr.Items.Count > 2) Number3 = ListBox_PhnNumbr.Items[2].ToString();
    if (ListBox_PhnNumbr.Items.Count > 3) Number4 = ListBox_PhnNumbr.Items[3].ToString();
    if (ListBox_PhnNumbr.Items.Count > 4) Number5 = ListBox_PhnNumbr.Items[4].ToString();
    MessageBox.Show("SOTA Configuration Saved Successfully"); 
    return new SOTA_whitelist_Number(Number1, Number2, Number3, Number4, Number5)
}

Where I'm missing? Why this exception is coming? Anyone help me

SaveXML Function is as follows:
class SaveXML
{
    public static void savedata(object obj, string filename)
    {
        XmlSerializer sr = new XmlSerializer(obj.GetType());
        TextWriter writer = new StreamWriter(filename);
        sr.Serialize(writer, obj);
        writer.Close();
    }
}
Posted
Updated 22-May-20 0:51am
v3
Rate this:
Please Sign up or sign in to vote.

Solution 1

Quote:
im getting some exception
Tells us nothing - we know you are getting an exception, but the actual error message is probably important - there are a lot of possible exceptions, and they generally all have different causes.

And this is probably related to your data and / or file system as well as your code, and we have no access to either.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
   
Comments
Garth J Lancaster 22-May-20 2:06am
   
bvgger ! where's that hidden camera ;-) .. now you're awake I'll just quit for the day
Rate this:
Please Sign up or sign in to vote.

Solution 2

(1) I don't believe you need the TextWriter

(2)
Quote:
writer.Close();
is just so passe these days - even for a boomer like me - 'using' is the way to go, so try this

XmlSerializer serializer = new XmlSerializer(obj.GetType());
using (StreamWriter writer = new StreamWriter(filename))
{
    serializer.Serialize(writer, obj);
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100