读取 XML 文件并将其用作数据库 - Windows Phone 7 应用程序
我有一个应用程序,它应该从 xml 文件读取数据,然后使用该数据。
如何在我的应用程序中导入 xml 文件(其代码是什么)以及如何使用该 xml 文件中的数据?
这是我使用的 xml 数据库的示例:
<Data>
<Animals>
<A>
<word>Ant</word>
<word>Aardwark</word>
</A>
<B>
<word>Bear</word>
<word>Boa</word>
</B>
</Animals>
</Data>
我还尝试使用此方法
XDocument loadedData = XDocument.Load("Data.xml");
从 xml 文件读取数据,但没有成功。
另外,我可以以什么形式使用 xml 数据?换句话说,xml 数据将采用字符串格式或“X-Something”格式?
更新:也许 Xml 反序列化对我有用?
先感谢您
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果“Data.xml”位于项目的根目录中,请确保将构建操作设置为
内容
并且您的代码应该可以工作。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 是您的朋友,将为您提供帮助就这样做!请注意,它将是只读的,除非您将其放置在 隔离存储。
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.
如果您已经拥有该文件并且每个应用程序实例都相同(假设您只需要读取它),则不需要 IsoStore。只需执行以下操作 马特说要快速获取内容。我建议将其反序列化为单独的类,以便您可以轻松地重用和修改数据。
现在,如果您想存储数据,稍后可以轻松序列化现有类并将其存储在本地。如果您想更深入地了解数据存储,可以使用 SQL CE,包含在 Mango 中,允许您操作 SDF 文件(顺便说一下,它可以与应用程序实例单独加载)。另外,一个好主意是研究 Sterling DB (将使用 IsoStore)。
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).
使用 System.XML 命名空间,使用以下代码。
您可以将 xNode 和 xNodeList 视为数组结果集,并使用 xNodeList[0] 等括号语法查看其内容。
Using the System.XML namespace, use the following code.
You can treat xNode and xNodeList kind of like array results sets and view their contents using the bracket syntax like xNodeList[0].