BeginnerGuy
BeginnerGuy

Reputation: 49

XML File Update

I am currently implementing a wpf application with a login system. To connect the server and call the database is slower than a file-based database, we think that xml file works fine for us.

My problem is actually the same as the problem which is stated in this topic:

When I add it to the project, it looks for it in the app root directory (WpfApplication1\WpfApplication1\myfile.xml). But then when I run the app, the function in charge to add a node works fine, but it saves the file to the WpfApplication1\WpfApplication1\bin\Debug\ directory ! Hence the "real" myfile.xml is not updated

WPF C# XML file added as resource

I tried the comment "If you want the xml to be loaded from disc (and modified), use Build Action = None and Copy = Copy Always." , but It didn't work for me.

I use System.Xml.Linq library and I try to update my XML file with this code piece:

  XDocument xmlDoc = XDocument.Load("users.xml");
               xmlDoc.Root.Add(
                    new XElement("user",
                        new XElement("username", this.textBox1.Text),
                        new XElement("password", this.textBox2.Text),
                        new XElement("ITAdmin", comboBox1.SelectedText == "Yes" ? 1 : 0)));

                xmlDoc.Save("users.xml");

My second problem is that I've added xml file as a resource to my project, because I don't want that it is reachable from application users, but I can't update the xml file again.

Upvotes: 2

Views: 656

Answers (2)

DeshDeep Singh
DeshDeep Singh

Reputation: 1853

try this



 private void AddToXmlLogInInfoDoc()
            {
                var x = this.DataContext as ViewModel.ViewModel;
                string path = System.AppDomain.CurrentDomain.BaseDirectory + "users.xml";
                XDocument  doc;
                doc = XDocument.Load(path);
                XElement ele = new XElement("LogUpdate",
                        new XElement("Id",
                            new XAttribute("Id", IdL.Text)),
                        new XElement("Name",
                            new XAttribute("Name", NameL.Text)),
                        new XElement("Password",
                            new XAttribute("Password", txtPassword.Password.ToString())),
                        new XElement("Department",
                            new XAttribute("Department", DeptL.Text)),
                        new XElement("Time",
                            new XAttribute("Time", x.LogTime.ToString())),
                        new XElement("TotalTime",
                            new XAttribute("TotalTime", x.TotalTime.ToString())),
                        new XElement("Log",
                            new XAttribute("Log", x.Log.ToString())));
                doc.Root.Add(ele);
                SaveLoginInfoToDisk(doc);
            }
private string GetLoginInfoFilePath()
        {
            return System.AppDomain.CurrentDomain.BaseDirectory + "users.xml";
        }

        private void SaveLoginInfoToDisk(XDocument document)
        {
            document.Save(GetLoginInfoFilePath());
        }

Upvotes: 0

user153923
user153923

Reputation:

If you specify "Copy Always", then every time you update/publish your website your XML document is going to be over written.

If you want this to be a database type of file, you would not want it overwritten with your XML file unless you made changes to it. Build Action "None" is good, but I would think you would want Copy to Output Director to be "Do not copy". You would need to manually FTP your file to the directory it is expected to be in.

Then, instead of having your XML file in the Resources, place it in your bin folder. Visitors to your website will not have access to that.

Upvotes: 1

Related Questions