求c#的熟悉xml的高手指教,小弟第一次接触 夜深了。。太长了没有看完。XML基本操作会吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 测试数据use testdbcreate table company(loc nvarchar(255),lastmod nvarchar(255))insert into companyselect 'http://111.htm','2010-04-06' union allselect 'http://222.htm','2010-04-07' union allselect 'http://333.htm','2010-04-08'相关代码using (SqlConnection conn = new SqlConnection("server=.;user id=sa;pwd=111;database=testdb;")){ conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "select * from company"; cmd.Connection = conn; SqlDataReader dr = cmd.ExecuteReader(); XDocument document = new XDocument(); XElement root = new XElement("urlset"); while (dr.Read()) { XElement element = new XElement("url"); element.Add(new XElement(dr.GetName(0), dr.GetValue(0)), new XElement(dr.GetName(1), dr.GetValue(1))); root.Add(element); } document.Add(root); document.Save(Server.MapPath("~/test.xml"));}结果:<?xml version="1.0" encoding="utf-8"?><urlset> <url> <loc>http://111.htm</loc> <lastmod>2010-04-06</lastmod> </url> <url> <loc>http://222.htm</loc> <lastmod>2010-04-07</lastmod> </url> <url> <loc>http://333.htm</loc> <lastmod>2010-04-08</lastmod> </url></urlset>测试数据库为testdb,数据库连接字符串修改成你自己的...操作XML用的是LINQ TO XML,需引用System.Xml.Linq这里只弄了两个字段,其余字段你自己处理。 http://www.cnblogs.com/malin/archive/2010/03/04/1678352.html 2楼的不错,用了linq但是,我现在迷惑了SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=pp;Integrated Security=True"); string zhujiedian = "select loc,lastmod,changefreq,priority,data from pp"; conn.Open(); SqlCommand cmd = new SqlCommand(zhujiedian,conn); SqlDataReader dr = cmd.ExecuteReader(); XDocument document = new XDocument(); XElement root = new XElement("urlset"); while (dr.Read()) { XElement element = new XElement("url"); for (int i = 0; i < dr.FieldCount; i++) { element.Add(new XElement(dr.GetName(i), dr.GetValue(i))); } root.Add(element); } document.Add(root); document.Save(@"c:\test.xml");这代码可以很方便的生成主要的节点,但是,问题来了,先给看一下现在的xml<?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 /> </url></urlset>data节需要插入很多信息,还要增加CDATA效果如图- <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>我该怎么去把这些信息插入到data节点里呢? 最简单的办法 直接把数据处理下 包裹下下呗,string data="<![CDATA[ "+data+"]]> ";OK 01 string myXml =02 @"<!--?xml version='1.0' encoding='utf-8'?-->03 <workingset>04 <data>05 </data>06 </workingset>";07 08 XmlDocument doc1 = new XmlDocument();09 10 doc1.LoadXml(myXml);11 12 XmlNode target = doc1.SelectSingleNode("WorkingSet/Data");13 14 if (target != null)15 target.AppendChild(doc1.CreateCDataSection("16 <customertag>Hello</customertag>17 "));使用 XDocumentview sourceprint?1 XDocument doc = XDocument.Parse(myXml, LoadOptions.SetLineInfo);2 3 XElement dataNode = doc.Descendants("Data").First();4 5 dataNode.Add(new XCData("6 7 <customertag>Hello</customertag>8 9 Console.WriteLine(doc.ToString());結果view sourceprint?1 <!--?xml version="1.0" encoding="utf-8"?-->2 <workingset>3 <data><!--[CDATA[<customertag-->Hello]]></data>4 </workingset> 你想把SQL Server数据库中的信息,查询出来,然后以XML文档保存?据我所知,直接用SQL Server数据库里面的函数转换,或者在.NET里面编程select数据后,构造xml文档。若你需要限制每个element的值类型和范围的话,应该需要*.xsd文件限制你生成的xml文档吧。 一条url节点难点,难道会有两条data节点? 看你的数据表结构,不应该把,你直接用title这些节点呗,把data节点去掉 动态数据存储问题 ASPxTabControl的问题 路径传值,产生“>]”(是竖的)的符号,是什么原因 可以获得局域网ip吗? url重写使图片不能正常显示的问题 请教一下这个一个系统如何构建好呢,讨论有分,顶者有分。 asp.net 引用mvc pagelist 报错 请问WebControl和HtmlControl 紧急求助 请问各位专家 动态增加DropDownList的Text值 如何将XML用XSL输出
(
loc nvarchar(255),
lastmod nvarchar(255)
)
insert into company
select 'http://111.htm','2010-04-06' union all
select 'http://222.htm','2010-04-07' union all
select 'http://333.htm','2010-04-08'
相关代码using (SqlConnection conn = new SqlConnection("server=.;user id=sa;pwd=111;database=testdb;"))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from company";
cmd.Connection = conn;
SqlDataReader dr = cmd.ExecuteReader(); XDocument document = new XDocument();
XElement root = new XElement("urlset"); while (dr.Read())
{
XElement element = new XElement("url");
element.Add(new XElement(dr.GetName(0), dr.GetValue(0)), new XElement(dr.GetName(1), dr.GetValue(1)));
root.Add(element);
}
document.Add(root);
document.Save(Server.MapPath("~/test.xml"));
}
结果:<?xml version="1.0" encoding="utf-8"?>
<urlset>
<url>
<loc>http://111.htm</loc>
<lastmod>2010-04-06</lastmod>
</url>
<url>
<loc>http://222.htm</loc>
<lastmod>2010-04-07</lastmod>
</url>
<url>
<loc>http://333.htm</loc>
<lastmod>2010-04-08</lastmod>
</url>
</urlset>
测试数据库为testdb,数据库连接字符串修改成你自己的...操作XML用的是LINQ TO XML,需引用System.Xml.Linq这里只弄了两个字段,其余字段你自己处理。
但是,我现在迷惑了SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=pp;Integrated Security=True");
string zhujiedian = "select loc,lastmod,changefreq,priority,data from pp";
conn.Open();
SqlCommand cmd = new SqlCommand(zhujiedian,conn);
SqlDataReader dr = cmd.ExecuteReader();
XDocument document = new XDocument();
XElement root = new XElement("urlset");
while (dr.Read())
{
XElement element = new XElement("url");
for (int i = 0; i < dr.FieldCount; i++)
{
element.Add(new XElement(dr.GetName(i), dr.GetValue(i)));
}
root.Add(element);
}
document.Add(root);
document.Save(@"c:\test.xml");这代码可以很方便的生成主要的节点,但是,问题来了,先给看一下现在的xml
<?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 />
</url>
</urlset>data节需要插入很多信息,还要增加CDATA
效果如图
- <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>我该怎么去把这些信息插入到data节点里呢?
string data="<![CDATA[ "+data+"]]> ";
OK
01 string myXml =
02 @"<!--?xml version='1.0' encoding='utf-8'?-->
03 <workingset>
04 <data>
05 </data>
06 </workingset>";
07
08 XmlDocument doc1 = new XmlDocument();
09
10 doc1.LoadXml(myXml);
11
12 XmlNode target = doc1.SelectSingleNode("WorkingSet/Data");
13
14 if (target != null)
15 target.AppendChild(doc1.CreateCDataSection("
16 <customertag>Hello</customertag>
17 "));使用 XDocument
view source
print?
1 XDocument doc = XDocument.Parse(myXml, LoadOptions.SetLineInfo);
2
3 XElement dataNode = doc.Descendants("Data").First();
4
5 dataNode.Add(new XCData("
6
7 <customertag>Hello</customertag>
8
9 Console.WriteLine(doc.ToString());結果
view source
print?
1 <!--?xml version="1.0" encoding="utf-8"?-->
2 <workingset>
3 <data><!--[CDATA[<customertag-->Hello]]></data>
4 </workingset>