Reading and using an XML file as a database - Wind

2019-07-02 01:08发布

问题:

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 format or an "X-Something" format?

Update: Maybe Xml Deserialization would work for me?

Thank you in advance

回答1:

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).



回答2:

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



回答3:

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.



回答4:

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].