日期:2014-05-17 浏览次数:20997 次
<?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>
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有自动推断功能:
DataSet ds = new DataSet(); ds.ReadXml(@"c:\test.xml");