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] + "-";
// }
//}
}
}
目前维护的开源产品:https://gitee.com/475660