2

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.

Community
  • 1
  • 1
BeginnerGuy
  • 49
  • 10
  • Completely unrelated: An XML file probably isn't the best place to store an unencrypted password. – Mike Eason Jul 24 '15 at 12:51
  • what is your recommend for storing this kind of information? – BeginnerGuy Jul 24 '15 at 13:13
  • This has already been answered [here](http://stackoverflow.com/questions/12657792/how-to-securely-save-username-password-local). – Mike Eason Jul 24 '15 at 13:22
  • I am already hashing the password information with sha256 but thank you in advance! – BeginnerGuy Jul 24 '15 at 13:27
  • Are you sure? Your code seems to suggest otherwise: `new XElement("password", this.textBox2.Text),` - You don't need to focus too much on hashing the password *right now*. You have other things to worry about, but it's something you should be aware of. – Mike Eason Jul 24 '15 at 13:37
  • This is the demo code for xml trials. So it doesn't include hashing method for now. If I succeed the xml updating, I'll update that part with my hash method. But thanks for your attention :) – BeginnerGuy Jul 24 '15 at 13:44

2 Answers2

1

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.

jp2code
  • 24,618
  • 35
  • 140
  • 254
  • Only the admin users are able to add user info to xml database, so the code-behind must have the ability to overwrite the xml file. When I add the xml file into the project (Visual Studio project), it is not created in the bin folder but in app's folder as default. But in debug mode, it creates a copy in /bin/Debug folder. Now I cannot change the file path of the base xml file. I don't think ftp is invalid for me, because it is not a web application, it is kind of windows forms application. – BeginnerGuy Jul 24 '15 at 13:12
  • If you specify a Build Action for the XML file, then it will need to go into your build directory, which is the bin folder. If you specify no action for the XML file and Copy Always, it should remain in the same location it is in your project. – jp2code Jul 24 '15 at 13:16
  • when BuildAction="None" and CopytoOutputDirector="Do not copy", it throws an exception which says that it is looking for a xml file in /bin/Debug folder, but it cannot find. – BeginnerGuy Jul 24 '15 at 13:25
  • :) That's because you said in the 1st comment above that you have it in your **app** folder. So, your code needs to look for it in this **app** folder and not the **bin** folder. The relative path would be "..\app\users.xml". The "..\" instructs it to look up one level. It depends which way you want to do it, but you must follow that decision in your coding. – jp2code Jul 24 '15 at 13:29
0
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());
        }
DeshDeep Singh
  • 1,565
  • 2
  • 18
  • 37
  • 1
    I think the method called "SaveLoginInfoToDisk" is the key part. I tried this, but it didn't work. – BeginnerGuy Jul 24 '15 at 13:00
  • I try to understand, LogInUpdater.xml is another file to copy the updated information? – BeginnerGuy Jul 24 '15 at 13:17
  • 1
    Since your XML file is set to Content, it is being copied to your "bin\Debug" folder. You will need to update **GetLoginInfoFilePath** to reflect this (i.e. `System.AppDomain.CurrentDomain.BaseDirectory + "Debug\LogInUpdater.xml";`) – jp2code Jul 24 '15 at 13:19
  • now check, I mistyped it according to other file I was using to check the case but you are using different one so change it to user,xml please – DeshDeep Singh Jul 24 '15 at 13:19
  • hope it is fine now and it works too, because it is sure shot. – DeshDeep Singh Jul 24 '15 at 13:22
  • if CopytoOutputDirectory=Do not copy, it is looking for xml file in bin/Debug/, if it is CopyToOutputDirectory=Copy always it creates a copy in bin/Debug/ folder and changes it. I write the same things in here, but nothing changed. I cannot find what the problem is. – BeginnerGuy Jul 24 '15 at 13:42
  • see copy always what it does is it create a new file everytime but copy newer will copy if it doesnot exists. What exactly you want? – DeshDeep Singh Jul 24 '15 at 14:19