I have a c# application that contains 2 methods. One to create an xml document, and one to load all of the nodes of the xml document to a tuple list.
using System.Xml;
namespace SOXMLExample
{
internal class Program
{
public static List<Tuple<string, string>> tupleXMLList = new List<Tuple<string, string>>();
static void Main(string[] args)
{
CreateXMLFile();
LoadContentsToTuple();
}
private static void CreateXMLFile()
{
string folderPath = AppDomain.CurrentDomain.BaseDirectory;
string xmlPath = Path.Combine(folderPath, "XMLFileToBeEncrypted.xml");
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xmlPath);
XmlElement xmlElement = xmlDocument.CreateElement(string.Empty, "usernames", string.Empty);
XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("FirstName");
xmlAttribute.Value = "John";
xmlElement.Attributes.Append(xmlAttribute);
xmlDocument.Save(xmlPath);
}
private static void LoadContentsToTuple()
{
tupleXMLList.Clear();
string folderPath = AppDomain.CurrentDomain.BaseDirectory;
string xmlPath = Path.Combine(folderPath, "XMLFileToBeEncrypted.xml");
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xmlPath);
XmlNodeList XMLNodes = xmlDocument.SelectNodes("//usernames");
foreach (XmlNode item in XMLNodes)
{
string FirstName = item.Attributes["FirstName"].Value;
string LastName = item.InnerText;
tupleXMLList.Add(new Tuple<string, string>(FirstName, LastName));
}
}
}
}
Currently, if the app were to be published, I can just go to the location of the xml file and open it to view all the contents. Instead, I want the program to create an XML file that cannot be opened directly by the user.
I have no clue how to do this since I was just introduced to XML a few weeks ago. I did some research and found that it is possible to encrypt the entire file and then decrypt it when the file needs to be read from. However, I feel like there is an easier way to keep the user from being able to open the file. Maybe something related to file permissions?