c#解析XML

1、引入using system.xml;

2、相应代码

XmlDocument doc = new XmlDocument();
doc.LoadXml(content);
XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
nsMgr.AddNamespace("a", "http://schemas.datacontract.org/2004/07/EnvCriteria.Model");
XmlNodeList dataNode = doc.SelectNodes("//a:Tab_AQIPublishInfo", nsMgr);
foreach (XmlNode node in dataNode)
{
	string positionName = node.SelectSingleNode("./a:PositionName", nsMgr).InnerText.Trim();
	if (!NameToStation.ContainsKey(positionName))
	{
		common.LOG.errorLog("a new station is added to Guangdong air quality. " + positionName);
		continue;
	}
	AirQuality airquality = new AirQuality();
	airquality.station_id = NameToStation[positionName].ID;
	airquality.PM25_Concentration = ToDouble(node.SelectSingleNode("./a:PM2_5", nsMgr).InnerText.Trim());
	airquality.PM10_Concentration = ToDouble(node.SelectSingleNode("./a:PM10", nsMgr).InnerText.Trim());
	airquality.NO2_Concentration = ToDouble(node.SelectSingleNode("./a:NO2", nsMgr).InnerText.Trim());
	airquality.SO2_Concentration = ToDouble(node.SelectSingleNode("./a:SO2", nsMgr).InnerText.Trim());
	airquality.O3_Concentration = ToDouble(node.SelectSingleNode("./a:O3", nsMgr).InnerText.Trim());
	airquality.CO_Concentration = ToDouble(node.SelectSingleNode("./a:CO", nsMgr).InnerText.Trim());
	airquality.time = Convert.ToDateTime(node.SelectSingleNode("./a:TimePoint", nsMgr).InnerText.Trim());
	list.Add(airquality);
}


转载请注明:康瑞部落 » c#解析XML

0 条评论

    发表评论

    电子邮件地址不会被公开。 必填项已用 * 标注