Read the .xml file on the Windows Phone
source link: https://www.codesd.com/item/read-the-xml-file-on-the-windows-phone.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Read the .xml file on the Windows Phone
I add .xml file to my roject. Now I need to open it. I try FileStream, StreamReader, IsolatedStorageFileStream. But there are get exception in each case. Somebody know how can i open local xml file and get data from it?
public static AllFlags Load()
{
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
AllFlags allFlags;
IsolatedStorageFileStream stream = storage.OpenFile(filename, FileMode.Open);
//StreamReader stream = new StreamReader(filename);
XmlSerializer xml = new XmlSerializer(typeof(AllFlags));
allFlags = xml.Deserialize(stream) as AllFlags;
stream.Close();
stream.Dispose();
return allFlags;
}
If you do not need xml specific information, but just read the contents of the file this is the easiest way: http://msdn.microsoft.com/en-us/library/system.io.file.readalltext.aspx
System.IO.File.ReadAllText(@"drive:\path\to\your\file.xml");
Otherwise there are speficied xml objects in the framework to do so. Typically you would use XmlDocument
. See the example below.
Obtained from http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.loadxml.aspx
using System;
using System.Xml;
public class Sample {
public static void Main() {
// Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.Load(@"drive:/path/to/you/file.xml");
//Get data from the Xml File
XmlNode Node = doc.SelectSingleNode("/apple/price");
// Add a price element.
XmlElement newElem = doc.CreateElement("price");
newElem.InnerText = "10.95";
doc.DocumentElement.AppendChild(newElem);
// Save the document to a file and auto-indent the output.
XmlTextWriter writer = new XmlTextWriter("data.xml",null);
writer.Formatting = Formatting.Indented;
doc.Save(writer);
}
}
Also see this link as its a good tutorial: http://www.codeproject.com/Articles/169598/Parse-XML-Documents-by-XMLDocument-and-XDocument
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK