使用嵌套元素将DataSet转换为XML [英] Converting DataSet into XML with nested elements

查看:195
本文介绍了使用嵌套元素将DataSet转换为XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个WebService,它将DataSet转换为XML。我的DataSet实际上是有类别的酒店列表。每个酒店都可以属于一个或多个类别。
我按照这里,它的效果很好,除了我没有得到我想要的结果。



这是我得到的:(示例1)

 < hotels> 
< hotel>
< name>希尔顿度假村< / name>
< category>
< catname> Hotel< / catname>
< / category>
< category>
< catname>度假村< / catname>
< / category>
< category>
< catname>高尔夫&水疗< / catname>
< / category>
< / hotel>
< hotel>
< name> Hyatt< / name>
< category>
< catname>度假村< / catname>
< / category>
< category>
< catname>高尔夫&水疗< / catname>
< / category>
< / hotel>
< / hotel>

但是,我想得到这样的东西:(例2)

 < hotels> 
< hotel>
< name>希尔顿度假村< / name>
< categories>
< catname> Hotel< / catname>
< catname>度假村< / catname>
< catname>高尔夫&水疗< / catname>
< / categories>
< / hotel>
< hotel>
< name> Hyatt< / name>
< categories>
< catname> Hotel< / catname>
< catname>高尔夫&水疗< / catname>
< / categories>
< / hotel>
< / hotel>

下面是我目前的代码生成XML,如例1所示:

  ... 
...
SqlDataAdapter hotelDA = new SqlDataAdapter(SQLHotels,SQLConn);
SqlDataAdapter catDA = new SqlDataAdapter(SQLCats,SQLConn);

DataSet ds = new DataSet(Hotels);

hotelDA.Fill(ds,Hotel);
catDA.Fill(ds,Category);

DataRelation SleepRel = ds.Relations.Add(Hotel,
ds.Tables [Sleep]。列[Id],
ds.Tables [类别]。列[AccID]);
SleepRel.Nested = true;
SQLConn.Close();
return ds;

SQLHotels - 选择所有酒店的SQL SELECT语句

SQLCats - 选择所有类别的SQL SELECT语句

Id - 酒店ID

AccId - 表中的酒店ID(外键)



我的问题:

1)它真的重要吗?一个结构比另一个更好吗?

2)可以在DataSet中的两个表之间建立关系,所以我得到像第二个(所需的)XML示例中的输出。

解决方案


一个结构比另一个更好?


更好的是什么?如果您想要将数据读回到 DataSet 中,则 DataSet 的格式非常好通过这个标准,至少要优于你提出的那个标准。



一般来说,当你处理一个XML文档时,你将会搜索它的元素你想使用XPath或Linq来处理。比较

  var categories = hotelElement.SelectNodes(category); 
var categories = hotelXElement.Elements(category);

with:

 code> var categories = hotelElement.SelectNodes(categories / category); 
var categories = hotelXElement.Elements(categories)。Element(category);

有什么中间的类别元素让你?编辑器中的XML看起来更好一些。这通常不是一个引人注目的优势,特别是如果它使XML更难处理。


有可能使两个表之间的关系在DataSet中,所以我得到的输出就像在第二个(所需的)XML示例中。


否。 DataSet 的序列化格式非常刚性地定义。它支持几个选项(嵌套,包括模式,diffgrams),但是格式不会改变(除非你保存为diffgrams,但是如果这样做,XML的外观在你的需求列表中很低) )。



尽管如此,您可以使用XSLT来更改格式。将其添加到身份转换

 < xsl:template match =hotel> 
< xsl:apply-templates select =* [name()!='category']/>
< xsl:if test =category>
< categories>
< xsl:apply-templates select =category/>
< / categories>
< / xsl:if>
< / xsl:template>


I am writing a WebService that is supposed to convert DataSet into XML. My DataSet is actually list of hotels with categories. Each hotel can belong to one or more categories. I followed an example from HERE and it works well except I don't get result that I would like.

This is what I get: (example 1)

<hotels>
    <hotel>
        <name>Hilton Resort</name>
        <category>
            <catname>Hotel</catname>
        </category>
        <category>
            <catname>Resort</catname>
        </category>
        <category>
            <catname>Golf & Spa</catname>
        </category>
    </hotel> 
    <hotel>
        <name>Hyatt</name>
        <category>
            <catname>Resort</catname>
        </category>
        <category>
            <catname>Golf & Spa</catname>
        </category>
    </hotel> 
</hotel>

but, I would like to get something like this: (example 2)

<hotels>
    <hotel>
        <name>Hilton Resort</name>
        <categories>  
            <catname>Hotel</catname>
            <catname>Resort</catname>
            <catname>Golf & Spa</catname>
        </categories>
    </hotel> 
    <hotel>
        <name>Hyatt</name>
        <categories>  
            <catname>Hotel</catname>
            <catname>Golf & Spa</catname>
        </categories>
    </hotel> 
</hotel>

Here is my current code that produces XML as in example 1:

        ...
        ...
        SqlDataAdapter hotelDA = new SqlDataAdapter(SQLHotels, SQLConn);
        SqlDataAdapter catDA = new SqlDataAdapter(SQLCats, SQLConn);

        DataSet ds = new DataSet("Hotels");

        hotelDA.Fill(ds, "Hotel");
        catDA.Fill(ds, "Category");

        DataRelation SleepRel = ds.Relations.Add("Hotel",
                 ds.Tables["Sleep"].Columns["Id"],
                 ds.Tables["Category"].Columns["AccID"]);
        SleepRel.Nested = true;
        SQLConn.Close();
        return ds;

SQLHotels - SQL SELECT statement that selects all hotels
SQLCats - SQL SELECT statement that selects all categories
Id - hotel ID
AccId - hotel ID in table Category (foreign key)

My questions:
1) Does it actually matter? Is one structure better then another one?
2) Is it possible to make relation between two tables in DataSet so I get output like it is in second (desired) XML example.

解决方案

Is one structure better than the other one?

Better for what? The DataSet's format is very good if you want to be able to read the data back into a DataSet, so by that standard, at least, it's superior to the one you're proposing.

Generally speaking, when you process an XML document, you will be searching it for the elements that you want to work with using XPath or Linq. Compare:

var categories = hotelElement.SelectNodes("category");
var categories = hotelXElement.Elements("category");

with:

var categories = hotelElement.SelectNodes("categories/category");
var categories = hotelXElement.Elements("categories").Element("category");

What does having intermediate categories elements get you? The XML looks a little bit nicer in an editor. That's generally not a compelling advantage, especially if it makes the XML a little harder to process.

Is it possible to make relation between two tables in DataSet so I get output like it is in second (desired) XML example.

No. The serialization format of the DataSet is very rigidly defined. It supports a couple of options (nesting, including the schema, diffgrams), but the format doesn't change (unless you're saving as diffgrams, but if you do that, how the XML looks is very low on your list of needs).

You can use XSLT to change the format pretty readily, though. Add this to the identity transform:

<xsl:template match="hotel">
   <xsl:apply-templates select="*[name() != 'category']"/>
   <xsl:if test="category">
      <categories>
         <xsl:apply-templates select="category"/>
      </categories>
   </xsl:if>
</xsl:template>

这篇关于使用嵌套元素将DataSet转换为XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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