在XML和C#中实现多对多映射的最佳方法是什么?

时间:2022-04-17 19:53:20

I have multiple data items, each with a value V that has multiple tags associated with it--t1,t2...tn. I have an XML file where I choose to store each item and the tags associated with it, e.g.

我有多个数据项,每个数据项都有一个值V,它有多个与之关联的标签 - t1,t2 ... tn。我有一个XML文件,我选择存储每个项目以及与之关联的标签,例如

<root>
<item>
<value>V1</value>
<tag>t11</tag>
<tag>t12</tag>
<tag>t13</tag>
</item>
<item>
<value>V2</value>
<tag>t21</tag>
<tag>t22</tag>
</item>
...
</root>

I want to be able to easily query for values V when I search for tags. How can I do this efficiently without writing entire libraries of code?

我希望能够在搜索标签时轻松查询值V.如何在不编写完整代码库的情况下有效地完成这项工作?

1 个解决方案

#1


1  

If you have control over your XML, I would restructure it like this:

如果您可以控制XML,我会像这样重构它:

<root>
    <item value="V1">
        <tag>t11</tag>
        <tag>t12</tag>
        <tag>t13</tag>
    </item>
    <item value="V2">
        <tag>t21</tag>
        <tag>t22</tag>
    </item>
</root>

This way you could simply write an XQuery like this to get all the tags for a given value:

这样你就可以简单地编写一个这样的XQuery来获取给定值的所有标记:

//item[@value="V2"]/tag

#1


1  

If you have control over your XML, I would restructure it like this:

如果您可以控制XML,我会像这样重构它:

<root>
    <item value="V1">
        <tag>t11</tag>
        <tag>t12</tag>
        <tag>t13</tag>
    </item>
    <item value="V2">
        <tag>t21</tag>
        <tag>t22</tag>
    </item>
</root>

This way you could simply write an XQuery like this to get all the tags for a given value:

这样你就可以简单地编写一个这样的XQuery来获取给定值的所有标记:

//item[@value="V2"]/tag