开发者

Reading and using an XML file as a database - Windows Phone 7 app

开发者 https://www.devze.com 2023-03-25 09:12 出处:网络
I have an app which should read the data from an xml file and then use that data. How can I import an xml file in my app (what\'s the code for that) and how can I use the data from that xml file?

I have an app which should read the data from an xml file and then use that data.

How can I import an xml file in my app (what's the code for that) and how can I use the data from that xml file?

Here's an example of the xml database I use:

<Data>
  <Animals>
    <A>
      <word>Ant</word>
      <word>Aardwark</word>
    </A>
    <B>
      <word>Bear</word>
      <word>Boa</word>
   </B>
  </Animals>
</Data> 

Also I tried this

XDocument loadedData = XDocument.Load("Data.xml"); 

to read the data from the xml file but didn't work.

Also the in what form can I use the xml data? In other words the xml data would be in a string 开发者_如何学Pythonformat or an "X-Something" format?

Update: Maybe Xml Deserialization would work for me?

Thank you in advance


If "Data.xml" is in the root of the project, make sure the Build Action is set to Content and your code should work.


Linq2XML is your friend, and will help you do just that! Mind you that it'll be read-only, unless you place it in the Isolated Storage.


No need for IsoStore if you already have the file and it is the same for every app instance (given that you only need to read it). Simply do what Matt said to quickly get the contents. I would recommend deserializing it to a separate class, so that you can easily reuse and modify the data.

Now, if you want to store the data, you can later easily serialize the existing class and store it locally. In case you want to go a bit deeper into data storage, you could use SQL CE, that is included in Mango and will allow you to manipulate SDF files (which, by the way, can be loaded separately with app instances). Also, a good idea would be to look into Sterling DB (will use IsoStore).


Using the System.XML namespace, use the following code.

XmlDocument xml = new XmlDocument();
xml.LoadXml("your string of xml");
XmlNode xNode = xml.SelectSingleNode("xpath to a single node");
XmlNodeList xNodeList = xml.SelectNodes("xpath to multiple nodes");

You can treat xNode and xNodeList kind of like array results sets and view their contents using the bracket syntax like xNodeList[0].

0

精彩评论

暂无评论...
验证码 换一张
取 消