c# - 将 MySql 查询保存到 XML 文件并避免重复

标签 c# mysql sql xml

您好,我有以下 C# 代码,可将数据保存到 xml 文件。

问题是它没有以我正在寻找的格式保存 xml,而且我似乎找不到一种方法来获取它。

C# 代码

string MyConString = "SERVER=localhost;" + "DATABASE=myvideos75;" + "UID=root;" + "PASSWORD=V0lc0m;";
string SQLSelect = "SELECT episode.idShow,tvshow.c00 as 'ShowName',episode.c12 as 'Season',XBMCPathReFact(episode.c18) as 'Path' FROM myvideos75.episode join tvshow on tvshow.idShow = episode.idShow where episode.c13 = 1 order by episode.idShow,episode.c12 ";
string _XMLFile = "test.xml";

MySqlConnection connection = new MySqlConnection(MyConString);
try
{
    MySqlCommand _MySqlSelect = new MySqlCommand(SQLSelect, connection);
    DataSet _DataSet1 = new DataSet("XBMC");
    MySqlDataAdapter _MySqlDataAdapter1 = new MySqlDataAdapter(_MySqlSelect);
    _MySqlDataAdapter1.Fill(_DataSet1,"Show");
    FileStream myFs = new FileStream(_XMLFile, FileMode.OpenOrCreate, FileAccess.Write);
    _DataSet1.WriteXml(myFs);
    myFs.Close();
}

catch (Exception ex)
{ MessageBox.Show(ex.Message.ToString()); }

它生成的 XML

<XBMC>
  <Show>
    <idShow>1</idShow>
    <ShowName>2 Broke Girls</ShowName>
    <Season>1</Season>
    <Path>\\10.0.0.3\tv\2 Broke Girls\Season1\</Path>
  </Show>
  <Show>
    <idShow>1</idShow>
    <ShowName>2 Broke Girls</ShowName>
    <Season>2</Season>
    <Path>\\10.0.0.3\tv\2 Broke Girls\Season 02\</Path>
  </Show>
  <Show>
    <idShow>31</idShow>
    <ShowName>Burn Notice</ShowName>
    <Season>2</Season>
    <Path>\\10.0.0.3\tv\Burn Notice\Season2\</Path>
  </Show>
  <Show>
    <idShow>31</idShow>
    <ShowName>Burn Notice</ShowName>
    <Season>3</Season>
    <Path>\\10.0.0.3\tv\Burn Notice\Season3\</Path>
  </Show>
 </XBMC>

我希望它生成这样的 XML。

<XBMC>
    <Show>
     <idShow>1</idShow>
     <ShowName>2 Broke Girls</ShowName>
     <Seasons>
        <Season>
         <Number>1</Number>
         <Path>\\10.0.0.3\tv\2 Broke Girls\Season1\</Path>
        </Season>
        <Season>
         <Number>2</Number>
         <Path>\\10.0.0.3\tv\2 Broke Girls\Season 02\</Path>
        </Season>
     </Seasons>
    </Show>
    <Show>
     <idShow>31</idShow>
     <ShowName>Burn Notice</ShowName>
     <Seasons>
        <Season>
         <Number>2</Number>
         <Path>\\10.0.0.3\tv\Burn Notice\Season2\</Path>
        </Season>
        <Season>
         <Number>3</Number>
         <Path>\\10.0.0.3\tv\Burn Notice\Season3\</Path>
        </Season>
     </Seasons>
     </Show>
</XBMC>

SQL select语句返回以下数据

enter image description here

感谢您的宝贵时间

最佳答案

好吧,我设法找到了解决方案,也许它可以帮助其他遇到类似问题的人。 我添加了一些评论来解释发生了什么。

string MyConString = "SERVER=10.0.0.3;" + "DATABASE=myvideos75;" + "UID=xbmc;" + "PASSWORD=xbmc;";
string SQLSelect = "SELECT tvshow.idShow as 'idShow',tvshow.c00 as 'ShowName',episode.c12 as 'Season',XBMCPathReFact(episode.c18) as 'Path' FROM myvideos75.episode join tvshow on tvshow.idShow = episode.idShow group by episode .idShow,episode.c12";

        try
        {if (File.Exists(_XMLFile))
            {File.Delete(_XMLFile);}
        }

        catch (Exception ex)
        { MessageBox.Show(ex.Message.ToString()); }

        MySqlConnection connection = new MySqlConnection(MyConString);

        try
        {
            MySqlCommand _MySqlSelect = new MySqlCommand(SQLSelect, connection);
            DataSet _DataSet1 = new DataSet("XBMC");
            MySqlDataAdapter _MySqlDataAdapter1 = new MySqlDataAdapter(_MySqlSelect);
            _MySqlDataAdapter1.Fill(_DataSet1,"Show");

            XDocument xdoc;
            XElement root;
            XElement tvshow;
            XElement TvShowElement;

            xdoc = new XDocument(new XElement("XBMC"));
            root = xdoc.XPathSelectElement("//XBMC");   

            // loop start fills stuff in 

            foreach (DataRow row in _DataSet1.Tables[0].Rows)
            {
                // Test if the show exists if not create it and add all the Element
                var ShowIDTest = xdoc.Element("XBMC").Elements("Show").Where(x => x.Element("idShow").Value.Equals(row["idShow"].ToString())).ToList();

                if (ShowIDTest.Count < 1)
                {
                    TvShowElement = new XElement("Show",
                                    new XElement("idShow", row["idShow"].ToString()),
                                    new XElement("ShowName", row["ShowName"].ToString()),
                                    new XElement("Seasons",
                                    new XElement("Season",
                                    new XElement("Number", row["Season"].ToString()),
                                    new XElement("Path", row["Path"].ToString()))));
                    root.Add(TvShowElement);
                }
                // The Show Exists , go to the show
                else
                {
                    string tvshowpath = "/XBMC/Show[idShow =\"" + row["idShow"].ToString() + "\"]";
                    tvshow = xdoc.XPathSelectElement(tvshowpath);
                    // Check if the show already has any season if not create it 
                    bool SeasonsTest = tvshow.Descendants("Seasons").Any();
                    if (!SeasonsTest)
                    {
                        TvShowElement = new XElement("Seasons",
                                        new XElement("Season",
                                        new XElement("Number", row["Season"].ToString()),
                                        new XElement("Path", row["Path"].ToString())));

                        tvshow.Add(TvShowElement);
                    }
                    // the show already has any season element so append the additional seasons
                    else
                    {
                        string tvshowseasonspath = "/XBMC/Show[idShow =\"" + row["idShow"].ToString() + "\"]/Seasons";
                        tvshow = xdoc.XPathSelectElement(tvshowseasonspath);

                        TvShowElement = new XElement("Season",
                                        new XElement("Number", row["Season"].ToString()),
                                        new XElement("Path", row["Path"].ToString()));

                        tvshow.Add(TvShowElement);
                    }
                  }
               }
            // Save the XML File   
            xdoc.Save(_XMLFile);
            MessageBox.Show("done");
        }
        catch (Exception ex)
        { MessageBox.Show(ex.Message.ToString()); }

关于c# - 将 MySql 查询保存到 XML 文件并避免重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19025478/

相关文章:

MySQL SELECT * WHERE 名称 = "something with a backslash\"

SQLite级联删除

c# - SQL C# 尝试访问前 10 个值

c# - 阅读窗口字体

c# - 使用精度值将整数转换为小数

c# - 调用 FinishedPlaying 事件时如何处理 AVAudioPlayer?

mysql使用交叉连接连接三个表错误

c# - 基于 C# 中使用的坐标,使用 System.Drawing.Printing 在多个页面上打印图形

mysql 选择日期时间在这一小时内的位置

sql - sql server中的简单like查询没有返回结果