SQL数据库到xml文档 [英] Sql database to xml document

查看:79
本文介绍了SQL数据库到xml文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助将数据从sql数据库导出到xml文档.

我的数据库内容是

I need help to export data from sql database to a xml doucment.

My database content is

SELECT      newsbee_ID, newsBee, link, title
FROM          tbl_newsBee



我需要将其中的数据导出到xml文件,而我的xml formate是



and i need to export the dat in that to an xml file and my xml formate is

<newses>
  <news link="URL 1" des="Description 1">Name 1</news>
  <news link="URL 2" des="Description 2">Name 2</news>
  <news link="URL 3" des="Description 3">Name 3</news>
  </newses>



此处des = newsBee,名称=标题,链接= link

如何使用c#在asp.net中做到这一点?

任何帮助对我来说都是好帮手.



Here des = newsBee and Name = title and link =link

How can I do it in asp.net using c#?

Any help will be a great hand for me.

推荐答案

^ ]可能会为您提供帮助.
This[^] might help you.


bijeesh06 开始,建议您使用datatable的WriteXML方法编写XML,那么您将无法获取数据我想以一种特定的格式.

要以每种格式检索数据,需要使用SQL Server提供的FOR XML EXPLICIT.

您可以在此处 [
As of bijeesh06 suggested if you will write XML using WriteXML method of datatable then you will not get data in a specific format I guess.

For retrieving data in As per format you want you need to go with FOR XML EXPLICIT provided by SQL Server.

You can get more detail into it HERE[^].


private void WriteXmlMethod()
        {
            string constring = ConfigurationManager.ConnectionStrings["NorthwindConnectionString1"].ToString();
            SqlConnection con = new SqlConnection(constring);
            con.Open();
            string query = "select * from tbl_newsBee";
            SqlCommand cmd = new SqlCommand(query, con);
            SqlDataReader dr = cmd.ExecuteReader();
            XDocument doc = new XDocument();
            XDeclaration declaration = new XDeclaration("1.0","utf-8","yes");
            doc.Add(declaration);
            XElement element1 = new XElement("Newses");
            XElement element2 = null;
            while (dr.Read())
            {
                element2 = new XElement("News",new XAttribute("link", dr[2].ToString()),new XAttribute("des",dr[1].ToString()));
                element2.Add(dr[3].ToString());
                element1.Add(element2);
            }
            con.Close();
            string filename = Server.MapPath("YourFolder/myXmlDoc.xml");
            doc.Save(filename);
        }



另一个答案



Another Answer

private void WriteXmlMethod()
        {
           
            string constring = ConfigurationManager.ConnectionStrings["ConnectionStringNews"].ToString();
            SqlConnection con = new SqlConnection(constring);
            con.Open();
            string query = "select * from tbl_newsBee";
            SqlCommand cmd = new SqlCommand(query, con);
            SqlDataReader dr = cmd.ExecuteReader();
            XmlDocument myXml = new XmlDocument();
            XmlElement el;
            XmlElement root = myXml.CreateElement("newses");
            myXml.AppendChild(root);
            while (dr.Read())
            {
                el = myXml.CreateElement("news");
                el.SetAttribute("link", dr[2].ToString());
                el.SetAttribute("des", dr[1].ToString());
                el.InnerText = dr[3].ToString();
                root.AppendChild(el);
            }
            con.Close();
            myXml.Save(favXml);
        }


这篇关于SQL数据库到xml文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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