I have a table (datatable)which looks like this
Hotelid Room# Description visitor Name amount
1 2 of 5 sam 10
1 2 of 5 sam 5
1 2 of 5 sam 50
1 2 of 8 james 50
1 2 of 8 james 50
1 2 of 6 justin 50
2 3 sm 4 john 5
2 4 al 3 jose 8
3 5 ms 2 tim 10
3 5 ms 7 tom 20
我希望将其转换成XML格式。我正在使用LINQ,但感到非常困惑和疲惫,不太明白怎么做。
<Hotels>
<Hotel id="1" room="2" description="of">
<Room="2" descr="of" visitor="5" name="sam"/>
<fine amount="10"/>
<fine amount="5"/>
<fine amount="50"/>
<Room="2" descr="of" visitor="8" name="james"/>
<fine amount="50"/>
<fine amount="50"/>
<Room="2" descr="of" visitor="6" name="justin"/>
<fine amount="50"/>
</hotel>
<Hotel id="2" room="3" description="sm">
<Room="3" descr="sm" visitor="4" name="john"/>
<fine amount="5"/>
</hotel>
<Hotel id="2" room="4" description="al">
<Room="4" descr="al" visitor="3" name="jose"/>
<fine amount="8"/>
</hotel>
<Hotel id="3" room="5" description="ms">
<Room="5" descr="ms" visitor="2" name="tim"/>
<fine amount="10"/>
<Room="5" descr="ms" visitor="7" name="tom"/>
<fine amount="20"/>
</hotel>
</Hotels>
这是我的代码样式:
var query =
from row in Hotels.AsEnumerable()
group row by new
{
Hotelid = row.Field<string>("Hotelid"),
room = row.Field<string>("room"),
descr = row.Field<string>("descr"),
}
into g
select new XElement("Hotel",
new XAttribute("Hotelid", g.Key.Hotelid),
new XAttribute("room", g.Key.room),
new XAttribute("desc", g.Key.desc),
from row in g
select new XElement(
"Room",
new XAttribute("room", row.Field<string>("room#")),
new XAttribute("desc", row.Field<string>("desc")),
new XAttribute("visitor", row.Field<string>("visitor")),
new XAttribute("name", row.Field<string>("name")),
from row in g
select new XElement(
"fine",
new XAttribute("amount", row.Field<string>("amount"))));
var document = new XDocument(new XElement("Hotels", query));
但是我得到了多个“room”节点具有相同的值。有任何帮助吗???? :(