c# - Read .xml file on windows phone -
i add .xml file roject. need open it. try filestream, streamreader, isolatedstoragefilestream. there exception in each case. know how can open local xml file , data 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) allflags; stream.close(); stream.dispose(); return allflags; }
if not need xml specific information, read contents of file 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 speficied xml objects in framework so. typically use xmldocument
. see example below.
obtained 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 xmldocument. xmldocument doc = new xmldocument(); doc.load(@"drive:/path/to/you/file.xml"); //get data xml file xmlnode node = doc.selectsinglenode("/apple/price"); // add price element. xmlelement newelem = doc.createelement("price"); newelem.innertext = "10.95"; doc.documentelement.appendchild(newelem); // save document file , auto-indent output. xmltextwriter writer = new xmltextwriter("data.xml",null); writer.formatting = formatting.indented; doc.save(writer); } }
also see link tutorial: http://www.codeproject.com/articles/169598/parse-xml-documents-by-xmldocument-and-xdocument
Comments
Post a Comment