2009-12-29 67 views
0

我有一个看起来像这样的XML文件。格式化XML数据,项目列表

<Almond> Roasted Almonds,Almond Extract,Almond Sauce,Almond Milk,Almond Cake,Almond Ice Cream,Almond Paste </Almond> 
<American Cheese> MilkWhey,Cheese Dip,Sliced Cheese </American Cheese> 
<Apple> Apple Sauce,Apple Pie,Pear,Apple Juice,Apple Cider,Apple Butter </Apple> 
<Avocado> Guacamole,California Sushi Rolls,Hass Avocado,Avocado Oil </Avocado> 
<Banana> Banana Bread,Banana Cream Pie,Banana Split,Banana Pudding </Banana> 

您是否建议以不同的方式对其进行格式化?

我需要获取与杏仁相关的所有食物,与美国奶酪,苹果等。

我将与vb.net来读取数据

+2

......甚至不需要是XML。 “成分:use1,use2,...”的格式就足够了。 – 2009-12-29 18:27:23

回答

2

而不必命令行分离值,我会有子标签<Item>什么的,所以解析器可以在处理它们时有一个镜头。它更详细,但更好的XML。

如果XML在这里不起作用,我会建议一些不像JSON那样冗长的东西。我没有看到标签和元数据在这里很好。

0

从你的XML你似乎需要某种“标记”。 如果我们假设你的XML用来存储成分,那么为什么不能有一个名为<ingredient/>

这里标签是在XML可能是什么样子的样本:

<recipelist> 
    <recipe name="Roasted Almonds"> 
     <ingredients> 
      <ingredient>Almond</ingredient> 
     </ingredients> 
    </recipe> 
    <recipe name="Almond Extract"> 
     <ingredients> 
      <ingredient>Almond</ingredient> 
      <ingredient>some other</ingredient> 
     </ingredients> 
    </recipe> 
</recipelist> 

然后你可以使用LINQ或一些其他的XML查询语言来查询与上面相同的结果。示例XPath查询将如下所示:

//recipelist/recipe[ingredients/ingredient/text()='Almond']/@name 

这会给所有具有“杏仁”成分的食谱。 这也可以为其他目的提供更灵活的XML格式,并且更容易从数据库生成。