In a WP7 project, you need to read a configuration file, which can be directly read without modification. You can use the following methods to read data:
Add the levels. xml file to the project in the following format:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <levels> <br/> <level> <br/> <levelid> 48 </levelid> <br/> <levelname> no route (note: this layout has no solution) </levelname> <br/> <content>, 1, 0, 4, 1, 3, 4 </content> <br/> <rcnt> 0 </rcnt> <br/> <minsteps> 0 </minsteps> <br/> <userid> 1 </userid> <br/> <username> admin </username> <br/> <difficulty> 7 </difficulty> <br/> </level> <br/> <level> <br/> <levelid> 252 </levelid> <br/> <levelname> 7 Series: seven-step poetry </levelname> <br/> <content>, 3, 4 </content> <br/> <rcnt> 255 </rcnt> <br/> <minsteps> 7 </minsteps> <br/> <userid> 765 </userid> <br/> <username> Aun </username> <br/> <difficulty> 0 </difficulty> <br/> </level> <br/> </levels>
Buildaction is set to content and then read in the Code:
Xdocument xdoc = xdocument. load ("alllevels. XML "); <br/> listlevels = new list <level> (); <br/> foreach (xelement element in xdoc. element ("levels "). descendants ("level") <br/>{< br/> level = new level (); <br/> level. levelid = int. parse (element. element ("levelid "). value); <br/> level. content = element. element ("content "). value; <br/> level. difficulty = int. parse (element. element ("difficulty "). value); <br/> // level. difficulty = 0; <br/> level. levelname = element. element ("levelname "). value; <br/> level. minsteps = int. parse (element. element ("minsteps "). value); <br/> level. rcnt = int. parse (element. element ("rcnt "). value); <br/> level. userid = int. parse (element. element ("userid "). value); <br/> level. username = element. element ("username "). value; <br/> listlevels. add (level); <br/>}
After xap is generated, use winrardecompress to view the levels. xml file. Sometimes it is not secure and can be compiled into the DLL. After setting buildaction to resource, you can compile the file into the DLL. If you are a beginner at WP7, you can easily see that the image cannot be displayed, because the image is generally set to content rather than resource.
This cannot be done directly after being set to resource.
Xdocument. Load ("alllevels. xml ");
You can write the complete URI path or read the stream, and use the following method: (both can be used)
// Use the load method to read stream when setting the resource attribute <br/> // stream = app. getresourcestream (New uri ("HRD; component/alllevels. XML ", urikind. relative )). stream; <br/> // xdocument xdoc = xdocument. load (Stream); </P> <p> // when set to the resource attribute, use the load method to read the URI <br/> URI uri = new uri ("/HRD; component/alllevels. XML ", urikind. relative); <br/> xdocument xdoc = xdocument. load (URI. tostring ());
In this way, the XML file is invisible to the compiled xap and has been compiled into the DLL.
BesidesXelement element in xdoc. element ("levels"). descendants ("level ")In addition to this reading method, XML serialization can also be used directly: for example:
// Another read method <br/> // URI uri = new uri ("myresource/mypeople. XML ", urikind. relative); <br/> // streamresourceinfo Sri = application. getresourcestream (URI); <br/> // If (SRI! = NULL) <br/> // {<br/> // xmlserializer serializer = new xmlserializer (typeof (list <person> )); <br/> // list <person> DATA = (list <person>) serializer. deserialize (SRI. stream); <br/> // This. listBox. itemssource = data; <br/> //}
Refer to the following articles:
Http://blog.csdn.net/lee353086/article/details/6674095
Http://www.silverlightchina.net/html/zhuantixilie/winphone7/2010/1207/3966.html? 1292140571
Http://www.cnblogs.com/tianyutingxy/archive/2011/02/27/1966267.html