首页 > 代码库 > Linq To Xml

Linq To Xml

以前在项目中解析xml中写的一个Linq解析xml的例子,摘录如下:

            string retValue = http://www.mamicode.com/"<root upmobile=\"12348\"><item><mobile>13361233119</mobile><time>2016-05-06 12:30:20</time>"
                     + "<content><CDATA>满意</CDATA></content></item><item><mobile>18612345678</mobile><time>2016-05-06 19:30:20</time>"
                     + "<content><CDATA>满意</CDATA></content></item></root>";
            XDocument doc = XDocument.Parse(retValue);
            var query = from d in doc.Descendants().Where(x => x.Name == "item")
                        select new
                        {
                            mobile = d.Element("mobile").Value,
                            time = d.Element("time").Value,
                            content = d.Element("content").Element("CDATA").Value
                        };
            foreach (var item in query)
            {
                Console.WriteLine(item.mobile);
                Console.WriteLine(item.time);
                Console.WriteLine(item.content);
            }
            Console.ReadKey();

 

Linq To Xml