日期:2014-05-18  浏览次数:20805 次

xml转化为json,谢谢
需要对xml文件中的信息进行提取,代码如何实现,谢谢。

xml文件如下:
XML code
<Book>
  <BookState>OK</BookState>
  <Bookshelf ID="1" ShelfInfo="ShelfInfoA" PID="0" SLevel="1" ShelfName="书架1" />
  <Bookshelf ID="2" ShelfInfo="ShelfInfo2" PID="1" SLevel="2" ShelfName="科普书架" >
    <BookInfo BookInfo='ssss'  BookName="地球人" BookID="1002" />
    <BookInfo BookInfo='dddd'  BookName="火星人" BookID="1003" />
    <BookInfo BookInfo='aaaa'  BookName="水星人" BookID="1004" />
    <BookInfo BookInfo='ffff'  BookName="土星人" BookID="1005" />
  </Bookshelf>
  <Bookshelf ID="3" ShelfInfo="ShelfInfo3" PID="1"  ShelfName="历史书架" />
  <Bookshelf ID="4" ShelfInfo="ShelfInfoB" PID="3"  ShelfName="中文历史书架" />
  <Bookshelf ID="5" ShelfInfo="XXShelfInfo" PID="4" ShelfName="近代中文历史书架" />
  <Bookshelf ID="7" ShelfInfo="ShelfInfo22" PID="1" ShelfName="文学书架" >
    <BookInfo BookInfo='eeeec' BookName="古代诗词" BookID="1001" />
  </Bookshelf>
  <Bookshelf ID="9" ShelfInfo="ShelfInfoW" PID="7" ShelfName="古代文学书架" />
</Book>


最后提取的信息组合为:

{ID:1,Info:'ShelfInfoA',PID:0,Name:'书架1'},
{ID:2,Info:'ShelfInfo2',PID:1,Name:'科普书架'},
{ID:1002,Info:'ssss',PID:2,Name:'地球人'},
{ID:1003,Info:'dddd',PID:2,Name:'火星人'},
{ID:1004,Info:'aaaa',PID:2,Name:'水星人'},
{ID:1005,Info:'ffff',PID:2,Name:'土星人'},
{ID:3,Info:'ShelfInfo3',PID:1,Name:'历史书架'},
{ID:4,Info:'ShelfInfoB',PID:3,Name:'中文历史书架'},
{ID:5,Info:'XXShelfInfo',PID:4,Name:'近代中文历史书架'},
{ID:7,Info:'ShelfInfo22',PID:1,Name:'文学书架'},
{ID:1001,Info:'eeeec',PID:7,Name:'古代诗词'},
{ID:9,Info:'ShelfInfoW',PID:7,Name:'古代文学书架'}

------解决方案--------------------
简单的方法,遍历xml进行拼接输出就可以了
或者找一些现成的

http://www.phdcc.com/xml2json.htm
------解决方案--------------------
http://www.lwolf.cn/blog/article/code/csharp-xml2json.htm