<?xml version="1.0" encoding="UTF-8" ?> 
<customers> 
  <customer code="ARUNI" name="PUMA ARGENTINA/UNISOL" region="Latin America" country="Argentina" /> 
  <customer code="AUPUA" name="Puma Australia Pty Ltd" region="APAC, Ex Japan" country="Australia" /> 
</customers> 請問大家: 
型如上面XML文件如何區裡面字段數據(customer code,name,region,country) 
多謝.

解决方案 »

  1.   


    DECLARE @idoc int
    DECLARE @doc varchar(1000)
    SET @doc ='
    <?xml version="1.0" encoding="UTF-8" ?> 
    <customers> 
      <customer code="ARUNI" name="PUMA ARGENTINA/UNISOL" region="Latin America" country="Argentina" /> 
      <customer code="AUPUA" name="Puma Australia Pty Ltd" region="APAC, Ex Japan" country="Australia" /> 
    </customers>'
    --Create an internal representation of the XML document.
    EXEC sp_xml_preparedocument @idoc OUTPUT, @doc
    -- Execute a SELECT statement that uses the OPENXML rowset provider.
    SELECT *
    FROM   OPENXML (@idoc, '/customers/customer',2)
             WITH (code      varchar(10)   '@code',
                   name      varchar(20)   '@name',
                   region    varchar(20)   '@region',
                   country   varchar(20)   '@country')
    /*
    code       name                 region               country              
    ---------- -------------------- -------------------- -------------------- 
    ARUNI      PUMA ARGENTINA/UNISO Latin America        Argentina
    AUPUA      Puma Australia Pty L APAC, Ex Japan       Australia(所影响的行数为 2 行)*/
      

  2.   


            DataSet ds = new DataSet();
            ds.ReadXml(Server.MapPath("db.xml"));
      

  3.   

    就是很普通的读取文件
      DataSet ds = new DataSet();
                ds.ReadXml(Server.MapPath("a.xml"));            
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
                    {
                         Response.Write(ds.Tables[0].Rows[i][j]);                                    }            }
      

  4.   

    恩,多謝大家.原本以為讀不到裡面字段,不能生成規則datatable.
    接分了.
      

  5.   

    因數據裡面有&符號  sql不能識別 & 要怎麼處理呢?