我正在编写一个应该将 DataSet 转换为 XML 的 WebService.我的数据集实际上是带有类别的酒店列表.每个酒店可以属于一个或多个类别.我从这里 并且效果很好,除非我没有得到我想要的结果.
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.
这就是我得到的:(示例 1)
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>
但是,我想得到这样的东西:(示例 2)
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>
这是我当前生成 XML 的代码,如示例 1 所示:
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 语句
SQLCats - 选择所有类别的 SQL SELECT 语句
Id - 酒店 ID
AccId - 表类别中的酒店 ID(外键)
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)
我的问题:
1) 这真的很重要吗?一种结构比另一种更好吗?
2) 是否可以在 DataSet 中的两个表之间建立关系,以便我得到的输出就像在第二个(所需的)XML 示例中一样.
一种结构比另一种更好吗?
Is one structure better than the other one?
为了什么更好?DataSet
的格式非常好,如果您希望能够将数据读回到 DataSet
中,因此至少按照该标准,它优于标准你在提议.
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.
一般来说,当您处理 XML 文档时,您将使用 XPath 或 Linq 在其中搜索要使用的元素.比较:
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");
与:
var categories = hotelElement.SelectNodes("categories/category");
var categories = hotelXElement.Elements("categories").Element("category");
中间 categories
元素能给你带来什么?XML 在编辑器中看起来更好一些.这通常不是一个引人注目的优势,特别是如果它使 XML 有点难以处理.
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.
是否可以在 DataSet 中的两个表之间建立关系,这样我就可以像在第二个(所需的)XML 示例中一样获得输出.
Is it possible to make relation between two tables in DataSet so I get output like it is in second (desired) XML example.
没有.DataSet
的序列化格式定义非常严格.它支持几个选项(嵌套,包括模式、diffgrams),但格式不会改变(除非你保存为 diffgrams,但如果你这样做,XML 的外观在你的需求列表中非常低).
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).
不过,您可以很容易地使用 XSLT 更改格式.将此添加到身份转换:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!