I've been trying to save an xml file to the phone for later use. I have tried to save it to Environment.SpecialFolder.MyDocuments, Environment.SpecialFolder.Personal, and Environment.SpecialFolder.LocalApplicationData. All of these work in the app session, as in I can save a file and read from it as long as the app is running. As soon as I close the app and reopen it, the app is supposed to read from the file and display information, but for some reason the file either does not exist (in which case the app creates a new blank one), or it's blank. I want to try and see what the file looks like itself, but I can't even find where it's stored because I just use the Environment.SpecialFolder... path. I've so far only tested this app on android if that helps.
Any help is greatly appreciated!
Here is the code to save data to the file checkins.xml:
//make new check in
//load the document
XmlDocument checkInDoc = new XmlDocument();
checkInDoc.Load(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "checkIns.xml");
//create the nodes
XmlNode checkInNode = checkInDoc.CreateElement("CheckIn");
XmlNode nameNode = checkInDoc.CreateElement("Name");
XmlNode timeNode = checkInDoc.CreateElement("Time");
//assign values to the nodes
nameNode.InnerText = "New Check In";
timeNode.InnerText = "9:00 PM";
//place nodes in document and save
checkInNode.AppendChild(nameNode);
checkInNode.AppendChild(timeNode);
checkInDoc.DocumentElement.AppendChild(checkInNode);
checkInDoc.Save(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "checkIns.xml");
And here is the code used to load the checkins from that file to a list:
List<CheckIn> tempList = new List<CheckIn>();
//Load Checkins from file into checkInList
XmlDocument checkInDoc = new XmlDocument();
checkInDoc.Load(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "checkIns.xml");
foreach(XmlNode node in checkInDoc.SelectNodes("CheckIns/CheckIn"))
{
tempList.Add(new CheckIn() { Name = node.SelectSingleNode("Name").InnerText, Time = node.SelectSingleNode("Time").InnerText });
}
I've actually solved this an entirely different way. I couldn't get saving/loading the file to work so I was able to use Application.Current.Properties which holds basic key/value pairs and is persistent between uses. I would highly recommend using this as it has worked perfectly for me.
Related
I have a list of websites that's been generated and stored into a text file. Now I'm trying to load that file so I can repeat the process of extracting website URLS.
Every time I run that application, HtmlAgilityPack.HtmlDocument is the only thing that's populated in the console window.
private static async void GetHtmlAsync1()
{
var doc = new HtmlDocument();
doc.Load(FilenameHere);
Console.WriteLine(doc);
}
Am I coming across this right?
Thanks
This is an example of loading text file full or URLs and reading their content. My test file is in the same location as my project files.
List<string> allUrls = File.ReadAllLines($#"{Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName}\test.txt").ToList();
HtmlDocument doc = new HtmlDocument();
foreach(string url in allUrls)
{
doc = new HtmlWeb().Load(url);
Console.WriteLine(doc.DocumentNode.InnerHtml);
}
Please note, i am only printing the entire website, you can use HtmlAgilityPack to actually scrape the data you are interested in (like pulling all the links, or specific class item.
Read in the lines from File
Load the data from URL using HtmlWeb.
Iterate through each URL and get what you need.
I'm making an application that loads and saves profile nodes to an external xml document in my output directory. It worked fine when I was opening it from my Assets folder but since that is read only (I think) I need to have it read and write from the output directory or another folder.
Like this:
XmlDocument users = new XmlDocument();
users.Load("users.xml");
However I get this error when this code runs:
"System.IO.FileNotFoundException: Could not find file "/users.xml"."
I've ticked the secondary storage permissions but i'm still a bit confused about just referencing a file in the output directory.
Would also appreciate the help for saving too as I assume the same error will occur:
users.DocumentElement.AppendChild(user);
users.Save("users.xml");
Thank you in advance.
In xamarin form you can try this code to load xml file from PCL project.
var assembly = typeof(TestClass).GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream(“PrjectName.FileName”);
using (var reader = new System.IO.StreamReader(stream))
{
var serializer = new XmlSerializer(typeof(List<BuildOptions>)); var listData = (List<T>)serializer.Deserialize(reader);
}
I have a VSTO document level customization that performs specific functionality when opened from within our application. Basically, we open normal documents from inside of our application and I copy the content from the normal docx file into the VSTO document file which is stored inside of our database.
var app = new Microsoft.Office.Interop.Word.Application();
var docs = app.Documents;
var vstoDoc = docs.Open(vstoDocPath);
var doc = docs.Open(currentDocPath);
doc.Range().Copy();
vstoDoc.Range().PasteAndFormat(WdRecoveryType.wdFormatOriginalFormatting);
Everything works great, however using the above code leaves out certain formatting related to the document. The code below fixes these issues, but there will most likely be more issues that I come across, as I come across them I could address them one by one ...
for (int i = 0; i < doc.Sections.Count; i++)
{
var footerFont = doc.Sections[i + 1].Footers.GetEnumerator();
var headerFont = doc.Sections[i + 1].Headers.GetEnumerator();
var footNoteFont = doc.Footnotes.GetEnumerator();
foreach (HeaderFooter foot in vstoDoc.Sections[i + 1].Footers)
{
footerFont.MoveNext();
foot.Range.Font.Name = ((HeaderFooter)footerFont.Current).Range.Font.Name;
}
foreach (HeaderFooter head in vstoDoc.Sections[i + 1].Headers)
{
headerFont.MoveNext();
head.Range.Font.Name = ((HeaderFooter)headerFont.Current).Range.Font.Name;
}
foreach (Footnote footNote in vstoDoc.Footnotes)
{
footNoteFont.MoveNext();
footNote.Range.Font.Name = ((Footnote)footNoteFont.Current).Range.Font.Name;
}
}
I need a fool proof safe way of copying the content of one docx file to another docx file while preserving formatting and eliminating the risk of corrupting the document. I've tried to use reflection to set the properties of the two documents to one another, the code does start to look a bit ugly and I always worry that certain properties that I'm setting may have undesirable side effects. I've also tried zipping and unzipping the docx files, editing the xml manually and then rezipping afterwards, this hasn't worked too well, I've ended up corrupting a few of the documents during this process.
If anyone has dealt with a similar issue in the past, please could you point me in the right direction.
Thank you for your time
This code copies and keeps source formatting.
bookmark.Range.Copy();
Document newDocument = WordInstance.Documents.Add();
newDocument.Activate();
newDocument.Application.CommandBars.ExecuteMso("PasteSourceFormatting");
There is one more elegant way to manage it based upon
Globals.ThisAddIn.Application.ActiveDocument.Range().ImportFragment(filePath);
or you can do the following
Globals.ThisAddIn.Application.Selection.Range.ImportFragment(filePath);
in order to obtain current range where filePath is a path to the document you are copping from.
I have a problem. Not very big but didn't find any answer.
I want to sore some key value pair in file Data.xml and I have saved it at root level.
When I am trying to use code below.
XDocument xmlDoc = new XDocument();
xmlDoc.Load("Data.xml");
Instead of checking at project root level it is checking for some other location. I tried current directory and environment.current path. But it didn't help.
I don't want to specify full path. Because when application will go live I don't want to change it.
I need to use data.xml so that if values got changed we will just replace the data.xml so get new values.
It's looking in the current directory that the application is running (bin\release or bin\debug). What you want to do is make sure that you have that XML file in your project set to Copy to Output Directory
You may want to prefix the name parameter with Application.StartupPath() for rigidity.
I got my answer from another post.
Click here
Below code can be used.
string dirpath = System.Web.HttpContext.Current.Server.MapPath("~/Data.xml");
XDocument xmlDoc = new XDocument();
xmlDoc.Load(dirpath);
What I am trying to do is when I press a button, I load an XML file and try to add nodes to it.
XmlDocument doc = new XmlDocument();
doc.Load("XMLFILE.xml");
XmlNode Tag1 = doc.CreateElement("Tag1");
XmlNode Tag2 = doc.CreateElement("Tag2");
Tag2.InnerText = food.Text;
Tag1.AppendChild(Tag2);
XmlNode Tag3 = doc.CreateElement("Tag3");
Tag3.InnerText = games.Text;
Tag1.AppendChild(Tag3);
XmlNode Tag4 = doc.CreateElement("Tag4");
Tag4.InnerText = life.Text;
Tag1.AppendChild(Tag4);
When I run the code and click the button the file is empty and only has 1 tag which is one I made when I first created the file. So how can I load an XML and add to it?
There are two issues with the code you posted:
You created a bunch of XML nodes but I don't see anywhere that you actually add them to the document you loaded. You need to call AppendChild() on the DocumentElement or some other node that's already in the file if you want your new nodes to appear in the XML tree.
Your code is loading an XML document from disk into memory and editing it, but you are never storing the XML document back to disk again. You need to call Save() on the updated document if you want to see the changes persisted back to your file.