LINQ 与 XML 结合使用
我们有一个项目,其中使用 LINQ 访问数据库层(MS SQL)。我们现在可以将数据库层更改为 XML 文件,并且仍然使用相同的 LINQ 访问它吗?
我们希望增加将数据存储在单个 XML 文件或一组 XML 文件中的可能性。
当前的 LINQ to SQL 代码如下所示:
result = (from e in db.Organizations
where e.Id == idOrganization
select e).SingleOrDefault();
或
result = (from e in db.Organizations
where e.Name.Trim().ToUpper() == organizationName.Trim().ToUpper()
&& e.Id!=idCurrentOrganization
select e).Count()>0;
We have a project in which access the DB layer (MS SQL) with LINQ. Can we now change our DB layer to XML file and still access it with the same LINQ?
We want add possibility to store data in a single XML file or a set of XML files.
Current LINQ to SQL code looks like:
result = (from e in db.Organizations
where e.Id == idOrganization
select e).SingleOrDefault();
or
result = (from e in db.Organizations
where e.Name.Trim().ToUpper() == organizationName.Trim().ToUpper()
&& e.Id!=idCurrentOrganization
select e).Count()>0;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
LINQ to SQL 和 LINQ to XML 有相似之处,但并不完全相同。 LINQ 查询可能可以编写为可以以相同的方式使用它们,但查询本身的代码可能需要更改。
您能否举例说明当前的 LINQ to SQL 代码是什么样的?包括您正在使用的数据类型以及您希望如何格式化 XML 的示例可能会帮助这里的人们提供更具体的指导。
编辑:您的第一个查询似乎返回一个对象。如果 LINQ to SQL 和 LINQ to XML 都返回相同的类型,那么您可以将它们交换出来,而无需更改使用结果的代码。
您的第二个查询似乎返回一个布尔值,因此相同的概念也适用于该查询。解决方案中其他地方的代码不会关心该布尔值是如何设置的。
迈克建议为数据访问层使用接口是可行的方法。对于您的 2 个示例,您将有一个包含 Organization 对象和 bool 的接口。您的 SQL 和 XML 实现会有所不同,但代码的所有其他部分只需要知道如何使用该接口。
LINQ to SQL and LINQ to XML have similarities, but they aren't identical. The LINQ queries could probably be written so that they could be used in the same way, but the code of the queries themselves would probably need to change.
Could you give some examples of what your current LINQ to SQL code looks like? Including examples of what kind of data you're using and how you want to format the XML would probably help people here give more specific pointers.
Edit: Your first query appears to return an object. If your LINQ to SQL and your LINQ to XML both return the same type, then you could swap them out without changing the code that consumes the result.
Your second query appears to return a bool, so the same concept applies to that one. Code elsewhere in your solution won't care how that bool was set.
Mike's suggestion of using an Interface for your data access layer is the way to go. For your 2 examples, you'd have an interface that contains an Organization object and a bool. Your SQL and XML implementations would be different, but every other part of your code would only need to know how to use the interface.
您还需要执行一个额外的步骤:在能够查询数据之前,您必须将 XML 加载到 XDocument(或者 XNode,如果我没记错的话)中。
之后,只需使用 LINQ to XML 语法选择树中的节点即可。
不幸的是,没有 LINQ 语法可以让您查询两者并让您透明地插入 XML 或 DB。
附言。当你选择节点时要注意XML命名空间,这可能会让人头疼!
PS2。抱歉,我没有方便的
PS3 代码。我建议您的数据库层从接口派生,这样您就可以
能够轻松地从 XML 切换到 DB,反之亦然
希望这会有所帮助
There's an extra step you will have to do: you will have to load the XML into a XDocument (or a XNode if I recall correctly) before being able to query you data.
After that it's simply a matter of selecting nodes in a tree with LINQ to XML syntax.
Unfortunately, there is no LINQ syntax that will let you query both and let you plug the XML or the DB transparently.
PS. Pay attention to the XML namespace when you will select nodes, it could be a headache!
PS2. Sorry I don't have code handy
PS3. I would suggest your database layer to derive from an interface, that way you will be
able to switch from XML to DB and vice-versa easily
Hope this helps