首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

关于xml读取,该怎么解决

2012-09-27 
关于xml读取XML code?xml version1.0 encodingutf-8 ?DMIFDataQuestion ID1tilte电影碟

关于xml读取

XML code
<?xml version="1.0" encoding="utf-8" ?><DMIFData>  <Question ID="1">    <tilte>电影碟中谍4的男主角是哪位?</tilte>    <option>      <answer id="A" >A.布鲁 斯威利斯</answer>      <answer id="B">B.汤姆 克鲁斯</answer>      <answer id="C">C.威尔 斯密斯</answer>    </option>    <rightanswers>B</rightanswers>    <image>图片地址</image>  </Question>  <Question ID="2">    <tilte>今年十一放几天假?</tilte>    <option>      <answer id="A" >A.10</answer>      <answer id="B">B.9</answer>      <answer id="C">C.8</answer>    </option>    <rightanswers>C</rightanswers>    <image>图片地址</image>  </Question></DMIFData>

就是类似于上面的xml文件 有10条数据,需要读取出来显示,有没有其他方式可以返回datatable dataset list之类的方法?
 看到过datatable.readxml不过这个提示错误不支持来自 Xml 的架构推断。
求帮助呃

[解决办法]
linq to xml可以方便操作
[解决办法]
C# code
XElement xEle = XElement.Load(@"C:\Documents and Settings\Administrator\桌面\Test.txt");                var result = xEle.Descendants("Question").Select(a => new                {                    id = a.Attribute("ID").Value,                    title = a.Element("tilte").Value,                    rightanswer = a.Element("rightanswers").Value,                    img = a.Element("image").Value,                    options = a.Element("option").Descendants("answer").Select(b => new {                         option=b.Attribute("id").Value,                        text=b.Value                    })                });
[解决办法]
DataSet有自动推断功能:
C# code
        DataSet ds = new DataSet();        ds.ReadXml(@"c:\test.xml"); 

热点排行