<folder title="我的书柜">
 ---<folder title="小说" id="1">
 ------<folder title="玄幻" ID="2" />
 ------<folder title="青春校园" ID="3" />
 ------<folder title="都市言情" ID="4" />
 ------<folder title="军事" ID="5" />
 ---</folder>
 ---<folder title="人物传记" id="6" />
 ---<folder title="科普" id="7" />
 ---<folder title="人文历史" id="8" />
 ---<folder title="励志成功" id="9" />
</folder>
<folder title="最近阅读">
      <folder bookstitle="小兵张嘎" />
</folder>如上面的xml格式,现在想用openxml来读取,并写入相应的sql表格中去,对于这种父子关系的xml,咋个去遍历呢,还有就是所有的title都不定,是程序生成的
表结构如下
CREATE TABLE [dbo].[folder](
[ID] [int] IDENTITY(1,1) NOT NULL,
[folderType] [int] NULL,                       foldertype就是最近阅读啊,书柜这些
[parentID] [int] NULL,                        用ID和parentID来做的父子关系,父ID是定为-1的
[title] [nchar](30) NULL,
[summary] [text] NULL,
[creatTime] [date] NULL,
[bookID] [int] NULL,

) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

解决方案 »

  1.   

    麻烦 lz 将与 xml 结构对应的关系表的内容列出,有些列与 xml 结构的对应关系不太清楚。-- 以下是基本方法
    declare @x xml;
    set @x=N'
    <folder title="我的书柜">
     <folder title="小说" id="1">
      <folder title="玄幻" id="2" />
      <folder title="青春校园" id="3" />
      <folder title="都市言情" id="4" />
      <folder title="军事" id="5" />
     </folder>
     <folder title="人物传记" id="6" />
     <folder title="科普" id="7" />
     <folder title="人文历史" id="8" />
     <folder title="励志成功" id="9" />
    </folder>
    ';
    select 
    t.c.value('./@id[1]','int') id,
    isnull(t.c.value('../@id[1]','int'),-1) parentid,
    t.c.value('./@title[1]','nvarchar(30)') title
    from @x.nodes('/folder/folder/descendant-or-self::*') t(c);
    /*
    1 -1 小说
    2 1 玄幻
    3 1 青春校园
    4 1 都市言情
    5 1 军事
    6 -1 人物传记
    7 -1 科普
    8 -1 人文历史
    9 -1 励志成功
    */
      

  2.   

    [ID] [int] IDENTITY(1,1) NOT NULL,  自增的字段
    [folderType] [int] NULL,   foldertype就是最近阅读啊,书柜这些,相当于是一个文件夹的分类
    [parentID] [int] NULL,     用ID和parentID来做的父子关系,父ID是定为-1的
    [title] [nchar](30) NULL,  父类以及书的分类都用这个字段表示的
    [summary] [text] NULL,     概要
    [creatTime] [date] NULL,    创建的时间
    [bookID] [int] NULL,       连接到book表的,通过bookid来取相应的书名
      

  3.   


    <folder bookstitle="小兵张嘎" /> 中的 bookstitle 在关系表中对应的是 [title] 列?那么其对应的 id 列是什么?是自动生成的?
    那么 <folder title="小说" id="1"> 中的 id 在关系表中对应的 id 列?这个 id 列不需要自动产生?关系表的 [folderType] 列为 int 类型,"我的书柜" 和 "最近阅读" 对应 int 值是什么?最好可以将 xml 与其对应的关系表中的行集提供一下,这样比较直观。
     
      

  4.   

    可能数据库设计的有点乱我有一个folder表,还有个book表,小兵张嘎是通过folder表里面的bookID在book表里面取得的
    能留个您的邮箱么?我截图给您看看,这里发不起图
      

  5.   

    怎么把
    /*
    1    -1    小说
    2    1    玄幻
    3    1    青春校园
    4    1    都市言情
    5    1    军事
    6    -1    人物传记
    7    -1    科普
    8    -1    人文历史
    9    -1    励志成功
    */这个值批量的update到sql里面去呢? 不是insert。比如有个userid=1