使用XPATH,LINQ等进行Xml映射 [英] Xml Mapping with XPATH, LINQ etc

查看:56
本文介绍了使用XPATH,LINQ等进行Xml映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要转换为本地对象的传入xml文档。 现在我有几个表来映射xml中的某些点映射到我的本地对象中的某些点,我主要使用xpath来描述xml中的位置。 

因此给出以下xml:

I have incoming xml documents that I need to transform into local objects.  Right now I have a few tables that map how certain points in xml map to certain points in my local objects and I primarily use xpath to describe the locations in xml. 

So given the following xml:

<?xml version="1.0" encoding="utf-8" ?> 
<menu>
	<menuItem>
		<text>My Books</text>
		<subMenu>
			<menuItem>
				<text>Designing Active Server Pages</text>
				<url>http://www.amazon.com/exec/obidos/tg/detail/-/0596000448/</url>
			</menuItem>
			<menuItem>
				<text>Teach Yourself ASP 3.0 in 21 Days</text>
				<url>http://www.amazon.com/exec/obidos/tg/detail/-/0672318636/</url>
			</menuItem>
		</subMenu>
	</menuItem>
</menu>


我会存储以下映射:

/ menu / menuItem / subMenu / menuItem / url

/menu/menuItem/subMenu/menuItem/url

OR

// url

这些映射的原因是存储在数据库中的开始是因为我们的对象将通过每个第三方系统API被序列化/反序列化到许多不同的第三方系统,因此我们的对象将为每个系统具有不同的序列化。此外,我们的对象通常比这个样本xml更复杂,并且本质上是复合的。

The reason these mappings are stored in a database to begin with is because our objects will be serialized/deserialized to many different 3rd party systems via each 3rd party systems API so our objects will have a different serialization for each system. Also our objects are generally more complex than this sample xml and are composite in nature.

我最近一直在关注LINQ,并想知道是否有更好的方法来使用LINQ,或者是否有一些其他新的.NET工具集比LINQ或XPath更适合实现我的最终结果?

I've been looking at LINQ lately and wondering if there is a better way to do this with LINQ, or is there some other new .NET toolset that would be better suited than either LINQ or XPath to achieve my end result?

任何想法或阅读建议将不胜感激。

Any ideas or reading suggestions would be greatly appreciated.

推荐答案

我不认为在这种情况下,LINQ将帮助您。首先,将LINQ查询存储在某些持久存储中是很困难的。

I don't think that LINQ will help you in this case. First of all it's kind of hard to store the LINQ queries in some persistant storage.

您是否考虑过某种转换。例如,XSLT允许您将任何输入(从任何系统)转换为预定义的标准格式(也称为XML),然后您可以从程序中使用(通过XLinq,Xml序列化,其他任何内容)。你可能有一个XSLT用于你可能拥有的每种输入,将它们存储在你的数据库或其他地方。

Have you considered some kind of transformation. For example XSLT would allow you to transform any input (from any system) to your predefined standard format (also XML), which you can then consume from your program (either through XLinq, Xml Serialization, anything else). You would have an XSLT for each kind of input you might have, store these either in your DB or somewhere else.

谢谢,


这篇关于使用XPATH,LINQ等进行Xml映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆