夜深了。。太长了没有看完。XML基本操作会吗?

解决方案 »

  1.   

    测试数据use testdbcreate table company
    (
    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这里只弄了两个字段,其余字段你自己处理。
      

  2.   

    http://www.cnblogs.com/malin/archive/2010/03/04/1678352.html
      

  3.   

    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节点里呢?
      

  4.   

    最简单的办法 直接把数据处理下  包裹下下呗,
    string data="<![CDATA[ "+data+"]]> ";
    OK
      

  5.   


    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>
      

  6.   

    你想把SQL Server数据库中的信息,查询出来,然后以XML文档保存?据我所知,直接用SQL Server数据库里面的函数转换,或者在.NET里面编程select数据后,构造xml文档。若你需要限制每个element的值类型和范围的话,应该需要*.xsd文件限制你生成的xml文档吧。
      

  7.   

    一条url节点难点,难道会有两条data节点? 看你的数据表结构,不应该把,你直接用title这些节点呗,把data节点去掉