Linq to Xml遍历

        private void GetAllValues(XElement doc)
        {
            ChartValues = new List<double>();
            ChartKeys = new List<string>();

            var myValues = from pv in doc.Descendants("value")
                           select pv.Value;
            foreach (var v1 in myValues)
                ChartValues.Add(Convert.ToDouble(v1));

            var myKeys = from pn in doc.Descendants("name")
                         //where pn.Value == "方欣科技"
                           select pn.Value;
            foreach (var v1 in myKeys)
                ChartKeys.Add(Convert.ToString(v1));
        }

 

 

调用:

        void ws_GetStockCompleted(object sender, GetStockCompletedEventArgs e)
        {

            if (e.Error == null)
            {
                XElement doc = XElement.Load(new StringReader(e.Result));
                GetAllValues(doc);
               

                //using (XmlReader Reader = XmlReader.Create(new StringReader(e.Result)))
                //{
                //    ChartValues = new List<double>();
                //    ChartKeys = new List<string>();
                //    for (int i = 0; i < 4; i++)
                //    {
                //        //Reader.ReadToFollowing("value");
                //        //ChartValues.Add(Convert.ToInt32(Reader.ReadInnerXml()));
                //        //Reader.ReadToFollowing("name");
                //        //ChartKeys.Add(Reader.ReadElementContentAsString());

                //        Reader.ReadToFollowing("value");
                //        ChartValues.Add(Reader.ReadElementContentAsDouble());

                //        //Stock1.Text += ChartValues[i] + "-";
                //    }
                //}
            }
        }

posted @ 2008-12-12 17:40  昕友软件开发  阅读(437)  评论(0编辑  收藏  举报
欢迎访问我的开源项目:xyIM企业即时通讯