C#之XML基本讀取(XmlDocument物件)
阿新 • • 發佈:2019-01-24
XmlDocument doc= new XmlDocument(); XmlElement rootElement; doc.LoadXml("<div>" + result.Replace(@" ", "") + "</div>");// 符XML無法解析,XML必須有根節點 rootElement = doc.DocumentElement; int tpCount =int.Parse(rootElement.SelectSingleNode("//div[@class='car_inf']/span[last()]").InnerText); string json = "{\"type\":\"success\",\"content\":{\"tpCount\":\"" + tpCount + "\",\"listInfo\":["; for (int i = 2; i <= tpCount+1; i++) { XmlNode trEle = rootElement.SelectSingleNode("//table[@class='table_bg']/tr["+i+"]"); json+="[\"\",\"" + "粵H00000" + "\",\"" + "B" + "\",\"" + "02" + "\",\""+ "3876"+ "\",\""; json += trEle.SelectSingleNode("td[2]").InnerText + "\",\"" + "\",\"\",\"";//文書號 json += trEle.SelectSingleNode("td[4]//tr[1]/td[2]").InnerText + "\",\"" + "惠州市\",\"";//帶中文字元違章時間 json += trEle.SelectSingleNode("td[4]//tr[2]/td[2]").InnerText + "\",\"";//違章地點 json += trEle.SelectSingleNode("td[4]//tr[3]/td[2]").InnerText + "\",\"";//違章程式碼 json += trEle.SelectSingleNode("td[4]//tr[4]/td[2]").InnerText + "\",\"" + "\",\"\",\"";//違章行為 json += trEle.SelectSingleNode("td[3]").InnerText + "00\",\"" + "\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"廣東省\",\"\",\"\",\"\"],";//罰款 } return json + "]}}"; foreach (XmlNode xn in trEles) { int i = 0; XmlNodeReader rd = new XmlNodeReader(xn); while (rd.Read()) { switch (rd.NodeType) { case XmlNodeType.Element: Response.Write(++i + ":" + rd.Name + "<br>"); break; case XmlNodeType.Text: Response.Write(++i + ":" + rd.Value + "<br>"); break; } } rd.Close(); }