c#根据mssql读取数据导出xml,求教,紧急!
- C# code
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;using System.Xml.Linq;namespace WindowsFormsApplication2{ public partial class Form1 : Form { SqlConnection conn = new SqlConnection(sqlconstring()); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private static string sqlconstring() { string connstr = "Data Source=.;Initial Catalog=pp;Integrated Security=True"; return connstr; } private static string mujiedian() { string zhujiedian = "select loc,lastmod,changefreq,priority from pp"; return zhujiedian; } private static string zijiedian() { string zijiedian = "select title,expirationdate,description,type,city,employer,email,jobfirstclass,jobsecondclass,education,experience,startdate,enddate,salary,industry,employertype,source,sourcelink from pp"; return zijiedian; } private static string lujing() { string dir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "/招聘信息.xml"; return dir; } public void jiajiedian() { conn.Open(); SqlCommand cmd = new SqlCommand(mujiedian(), conn); SqlDataReader dr = cmd.ExecuteReader(); XDocument document = new XDocument(); XElement root = new XElement("urlset"); XElement element = new XElement("url"); while (dr.Read()) { for (int i = 0; i < dr.FieldCount; i++) { element.Add(new XElement(dr.GetName(i), dr.GetValue(i))); } dr.Dispose(); { SqlCommand cmd2 = new SqlCommand(zijiedian(), conn); SqlDataReader dr2 = cmd2.ExecuteReader(); while (dr2.Read()) { for (int j = 0; j < dr2.FieldCount; j++) { element.Add(new XElement("data", new XElement("display", new XElement(dr2.GetName(j),dr2.GetValue(j))))); } } } root.Add(element); } document.Add(root); document.Save(lujing()); conn.Close(); } private void button1_Click(object sender, EventArgs e) { jiajiedian(); } }}这是代码...但是报错了,我想实现的效果如下
- XML code
<?xml version="1.0" encoding="utf-8" ?> - <urlset>- <url> <loc>http://jobs.zhaopin.com/P5/CC0000/1824/J902/500/CC000018244J90250002000.htm</loc> <lastmod>2010-04-06</lastmod> <changefreq>always</changefreq> <priority>1.0</priority> - <data>- <display>- <title>- <![CDATA[ HPSW-Senior R&D Manager-Shanghai ]]> </title> <expirationdate>2011-01-17</expirationdate> - <description>- <![CDATA[ HP is a leading global provider of products, technologies, solutions and services to consumers and business. The company's offerings span IT infrastructure, personal computing and access devices, global services, and printing. Our $4 billion annual R&D investment fuels the invention of products, solutions and new technologies. We invent, engineer that drive business value, improve the lives of our customers. ]]> </description> <type>社会招聘</type> <city>上海</city> - <employer>- <![CDATA[ 惠普公司 ]]> </employer>- <email>- <![CDATA[ https://hp.taleo.net/careersection/2/jobdetail.ftl?lang=en&job=1720103&media_id=1009&src=Zhaopin ]]> </email>- <jobfirstclass>- <![CDATA[ 计算机软、硬件/互联网/IT ]]> </jobfirstclass>- <jobsecondclass>- <![CDATA[ 高级软件工程师 ]]> </jobsecondclass> <education>本科</education> <experience>5年以上</experience> <startdate>2010-04-05</startdate> <enddate>2011-01-17</enddate> <salary>面议</salary> - <industry>- <![CDATA[ IT服务(系统/数据/维护)/多领域经营,计算机硬件及网络设备 ]]> </industry> <employertype>合资</employertype> <source>智联招聘</source> - <sourcelink>- <![CDATA[ http://www.zhaopin.com/ ]]> </sourcelink> </display> </data> </url>- <url> <loc>http://jobs.zhaopin.com/P2/CC1204/9901/J902/501/CC120499019J90250131000.htm</loc> <lastmod>2010-05-13</lastmod> <changefreq>always</changefreq> <priority>1.0</priority> - <data>- <display>- <title>- <![CDATA[ 城市销售代表(温州/宁波) ]]> </title> <expirationdate>2010-07-25</expirationdate> - <description>- <![CDATA[ 岗位职责:分别负责东芝宏产品温州及周边和宁波及周边区域销售,包括该品牌数码港,4-6级指标销售。岗位要求:1,本科以上,一年以上IT或快销销售经验,有渠道和区域销售经验者优先考虑;2,本科以上,一年以上IT或快销销售经验,有渠道和区域销售经验者优先考虑 ]]> </description> <type>社会招聘</type> <city>宁波</city> - <employer>- <![CDATA[ 杭州神州数码有限公司 ]]> </employer>- <email>- <![CDATA[ ]]> </email>- <jobfirstclass>- <![CDATA[ 销售业务 ]]> </jobfirstclass>- <jobsecondclass>- <![CDATA[ 渠道/分销专员 ]]> </jobsecondclass> <education>本科</education> <experience>1-3年</experience> <startdate>2010-05-13</startdate> <enddate>2010-07-25</enddate> <salary>1000-50000元/月</salary> - <industry>- <![CDATA[ 计算机硬件及网络设备,计算机软件,IT服务(系统/数据/维护)/多领域经营,通讯(设备/运营/增值服务) ]]> </industry> <employertype>合资</employertype> <source>智联招聘</source> - <sourcelink>- <![CDATA[ http://www.zhaopin.com/ ]]> </sourcelink> </display> </data> </url> <url>...</url> ... </urlset>
这些节点 都是数据库表里的字段
只需要把数据库里的内容读取到每个节点就好了
其中,像这样的
<![CDATA[ HPSW-Senior R&D Manager-Shanghai
]]>
可以用 XCData对象增加描述
..........
..........
看明白了吗?
要实现这样的效果,代码需要怎样改进?
大家一起研究!
求高人指点
[解决办法]
可以直接用 sql 生成xml sql语句后面指定 for xml 就可以了
或直用 dataset 的 writeXml方法 参数可以是文件、流等。。可以查下msdn
[解决办法]
XElement s = new XElement();
XCData dd=new XCData("内容");
s.Name = "Title";
s.SetValue(dd);
XElement s2 = new XElement();
s2.Name = "education";
s2.Value="本科";
明白?