需要帮助将DataTable转换为XML VB.NET [英] Need help converting DataTable to XML VB.NET

查看:335
本文介绍了需要帮助将DataTable转换为XML VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助将DataTable转换为XML。我已经使用LINQ,但我不能得到完全一样的。我做了一张照片,让你很容易理解。 XML需要以下面的某种格式,所以我不能只使用dt.writexml()。艺术家ID需要自动编号。歌曲由艺术家组合。喜欢Linq coz的解决方案,这是我在整个项目中使用的,但我无法在这里找到我想要的东西。列名称是已知的,因此您可以在代码中使用这样的名称。 row.Field(Of String)(title)

I need help converting DataTable to XML. I have done it using LINQ but I can't get exactly as I have to. I made a picture so that you can easily understand. The XML needs to be in certain format like below so I can't just use dt.writexml(). Artist ID needs to auto number. Songs are groupped by Artist. Prefer a solution in Linq coz that's what I have used throughout the project but I coundn't manage to get what I want here. The columns names are known so you can use something like this in the code. row.Field(Of String)("title")

非常感谢。我是认真的。对不起,英文不好。

Thanks a lot . I mean it. Sorry for poor english.

CreateDatatable - 这个简单的代码应该创建一个datatable

CreateDatatable - this simple code should create a datatable

            Dim dTable As New DataTable
            dTable.Columns.Add("Title")
            dTable.Columns.Add("Artist")
            dTable.Columns.Add("Album")

            dTable.Rows.Add("Baby one more time", "Britney Spears", "Baby one more time")
            dTable.Rows.Add("Crazy", "Britney Spears", "Best of")
            dTable.Rows.Add("Every time", "Britney Spears", "Best of")
            dTable.Rows.Add("Black and White", "Michael Jackson", "Best of")
            dTable.Rows.Add("You are not alone", "Michael Jackson", "Best of")
            dTable.Rows.Add("Smile", "Michael Jackson", "Best of")

我现在有什么。它将它的datatable转换为xml而没有分组和专辑索引。

what I have at the moment. It will convert it datatable to xml without the groupping and album index.

      Dim xmlDoc As New XDocument(
      From row In dt.Rows
      Select XElement("SONG",
      From column In dt.Columns
            Select
                New XAttribute(column.Name, row.Item(column.Name))
             )
      )

我还有更多的代码..这将首先查询创建的xml并进行分组,但仍将歌曲元素中的album =albumname作为属性。而且它应该只是从datatable到xml的一个查询。我讨厌不得不再次查询xml来重新调整它。

well .. i also have some more code .. that will query first created xml and do the grouping but still having album="albumname" in the song element as attribute. And it should be just one query from datatable to xml .. i hate having to query against xml again to just refomat it.

    Dim replacement = New XDocument(New XElement("root", 
    original.Descendants("Song")
    .GroupBy(Function(x) Convert.ToString(x.Element("artist").value))
    .[Select](Function(songsForArtist, index) 
     New XElement("artist", New XAttribute("id", index + 1),
     New XAttribute("name", songsForArtist.Key), songsForArtist))))


推荐答案

我希望您可以将其转换为VB.NET

I hope you can convert it to VB.NET

    using System;
    using System.Linq;
    using System.Data;
    using System.Xml.Linq;

    namespace ConsoleApplication3
    {
        class Program
        {
            static void Main(string[] args)
            {
                var dTable = new DataTable();
                dTable.Columns.Add("Title");
                dTable.Columns.Add("Artist");
                dTable.Columns.Add("Album");

                dTable.Rows.Add("Baby one more time", "Britney Spears", "Baby one more time");
                dTable.Rows.Add("Crazy", "Britney Spears", "Best of");
                dTable.Rows.Add("Every time", "Britney Spears", "Best of");
                dTable.Rows.Add("Black and White", "Michael Jackson", "Best of");
                dTable.Rows.Add("You are not alone", "Michael Jackson", "Best of");
                dTable.Rows.Add("Smile", "Michael Jackson", "Best of");

                var query = dTable.AsEnumerable().
                    GroupBy(row => row.Field<string>("Artist")).
                    Select(
                        (grp, i) => new XElement("Artist",
                            new XAttribute("ID", i + 1),
                            new XAttribute("ARTISTNAME", grp.Key),
                                grp.Select(song => new XElement("SONG",
                                    new XAttribute("artistID", i + 1),
                                    new XAttribute("title", song.Field<string>("Title")),
                                    new XAttribute("album", song.Field<string>("Album"))
                                    )
                                )
                        )
                    );

                var xml = new XElement("Music", query);
            }
        }
    }

这篇关于需要帮助将DataTable转换为XML VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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