Пример XML
<datagetway act="getcavelist"> <info> datagetway ver 1.0 act=getcavelist</info> <caves> <cave cavename="п.Камызякская" datecrt="0" dateupd="0" id="2"> <cavedesc>Очень красивая пещера.</cavedesc> <sessions> <session datecrt="0" dateupd="0" id="2" isused="0" sessionname="KAMIZAK"> <sessiondesc>Длина: 272 Глубина: 74</sessiondesc> </session> </sessions> </cave> <cave cavename="п.Пять Озер" datecrt="0" dateupd="0" id="1"> <cavedesc>Пещера еще до конца не изучена.</cavedesc> <sessions> <session datecrt="0" dateupd="0" id="1" isused="0" sessionname="FLIKE1"> <sessiondesc>Длина: 811 Глубина: 57</sessiondesc> </session> </sessions> </cave> </caves> </datagetway>
Код обработчика:
string sResult = e.Result.Trim(); // текст в формате XML StringReader stream = new StringReader(sResult); XmlReader reader = XmlReader.Create((TextReader)stream); string act = ""; string info = "", node = "", nodecv = "", nodecv2 = "", nodecv3 = ""; #region Пример XML //<?xml version="1.0" encoding="utf-8" ?> //<datagetway act="getcavelist" > //<info> datagetway ver 1.0 act=getcavelist</info> //<caves> // <cave id="2" cavename="п.Камызякская" datecrt="0" dateupd="0"> // <cavedesc>Очень красивая пещера.</cavedesc> // <sessions> // <session id="2" sessionname="KAMIZAK" datecrt="0" dateupd="0" isused="0"> // <sessiondesc></sessiondesc> // </session> // </sessions> // </cave> // <cave id="1" cavename="п.Пять Озер" datecrt="0" dateupd="0"> // <cavedesc>Пещера еще до конца не изучена.</cavedesc> // <sessions> // <session id="1" sessionname="FLIKE1" datecrt="0" dateupd="0" isused="0"> // <sessiondesc></sessiondesc> // </session> // </sessions> // </cave> //</caves> //</datagetway> #endregion Пример XML cavelist = new List<Caves>(); Caves cv = new Caves(); SessionCave ssc = new SessionCave(); int cvint = -1, sscint = -1; List<PiketListItem> listPk = new List<PiketListItem>(); // Журнал пикетажки PiketListItem itemPk = new PiketListItem(); //строка-пикет #region Чтение XML while (reader.Read())//Чтение XML { //textBoxLog.Text += "XML Read " + reader.Name + "; cvint=" + cvint.ToString() + "\r\n"; #region NodeType == Element if (reader.NodeType == XmlNodeType.Element) //Если элемент { #region datagetway if (reader.Name == "datagetway") { act = reader.GetAttribute("act"); textBoxLog.Text += "XML datagetway act = " + act + "\r\n"; } #endregion datagetway #region info else if (reader.Name == "info") { node = reader.Name; } #endregion info #region caves else if (reader.Name == "caves") { node = "caves"; textBoxLog.Text += "XML node = " + node + "\r\n"; } #region cave else if (reader.Name == "cave" && node == "caves") { nodecv = reader.Name; cv = new Caves(); cv.id = Convert.ToInt32(reader.GetAttribute("id")); cv.cavename = reader.GetAttribute("cavename"); cv.datecrt = reader.GetAttribute("datecrt"); cv.dateupd = reader.GetAttribute("dateupd"); cavelist.Add(cv); cvint = cavelist.Count - 1; textBoxLog.Text += "XML nodecv = " + nodecv + "\r\n"; } #region cavedesc else if (reader.Name == "cavedesc" && node == "caves" && nodecv == "cave") { nodecv2 = reader.Name; } #endregion cavedesc #region sessions else if (reader.Name == "sessions" && node == "caves" && nodecv == "cave") { nodecv2 = reader.Name; cavelist[cvint].sessions = new List<SessionCave>(); } #region session else if (reader.Name == "session" && node == "caves" && nodecv == "cave" && nodecv2 == "sessions") { ssc = new SessionCave(); ssc.id = Convert.ToInt32(reader.GetAttribute("id")); ssc.name = reader.GetAttribute("sessionname"); ssc.datecrt = reader.GetAttribute("datecrt"); ssc.dateupd = reader.GetAttribute("dateupd"); ssc.isused = reader.GetAttribute("isused"); cavelist[cvint].sessions.Add(ssc); textBoxLog.Text += "XML session = " + ssc.name + "\r\n"; sscint = cavelist[cvint].sessions.Count - 1; } #region sessiondesc else if (reader.Name == "sessiondesc" && node == "caves" && nodecv == "cave" && nodecv2 == "sessions") { nodecv3 = reader.Name; } #endregion sessiondesc #endregion session #endregion sessions #endregion cave #endregion caves #region addpiket if (reader.Name == "addpiket") { string sblockid = reader.GetAttribute("blockid"); string sact = reader.GetAttribute("act"); int noBl = -1; int i = -1; foreach (BlockGet bl in blocks) { i++; if (bl.blockId == sblockid) noBl = i; } if (noBl > -1) blocks.RemoveAt(noBl); textBoxLog.Text += "осталось блоков:" + blocks.Count; if (blocks.Count == 0) { Uri uri; WebClient wc; uri = new Uri(getwayURL.Text + "?act=commitpkt&sessionid=" + sessionID, UriKind.Absolute); wc = new WebClient(); textBoxLog.Text += "DownloadStringCompleted... commit" + "\r\n"; wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCompletedHndlr); textBoxLog.Text += "DownloadStringAsync... \r\n"; wc.DownloadStringAsync(uri, "POST"); } } #endregion addpiket #region addpiketxml if (reader.Name == "addpiketxml") { string sblockid = reader.GetAttribute("blockid"); string sact = reader.GetAttribute("act"); int noBl = -1; int i = -1; foreach (BlockGet bl in blockXML) { i++; if (bl.blockId == sblockid) noBl = i; } if (noBl > -1) blockXML.RemoveAt(noBl); textBoxLog.Text += "осталось блоков:" + blockXML.Count; if (blockXML.Count == 0) { Uri uri; WebClient wc; uri = new Uri(getwayURL.Text + "?act=commitpkt&sessionid=" + sessionID, UriKind.Absolute); wc = new WebClient(); textBoxLog.Text += "DownloadStringCompleted... commit" + "\r\n"; wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCompletedHndlr); textBoxLog.Text += "DownloadStringAsync... \r\n"; wc.DownloadStringAsync(uri, "POST"); } } #endregion addpiketxml #region addstationxml if (reader.Name == "addstationxml") { string sblockid = reader.GetAttribute("blockid"); string sact = reader.GetAttribute("act"); int noBl = -1; int i = -1; foreach (BlockGet bl in blockStsXML) { i++; if (bl.blockId == sblockid) noBl = i; } if (noBl > -1) blockStsXML.RemoveAt(noBl); textBoxLog.Text += "осталось блоков:" + blockStsXML.Count; if (blockStsXML.Count == 0) { Uri uri; WebClient wc; uri = new Uri(getwayURL.Text + "?act=commitsts&sessionid=" + sessionID, UriKind.Absolute); wc = new WebClient(); textBoxLog.Text += "DownloadStringCompleted... commit" + "\r\n"; wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCompletedHndlr); textBoxLog.Text += "DownloadStringAsync... \r\n"; wc.DownloadStringAsync(uri, "POST"); } } #endregion addstationxml #region commitpiket if (reader.Name == "commitpiket") { textBoxLog.Text += "COMMIT PICKET"; textBlockStat.Text = "Данные переданы"; textBlockStat.Text = "Передача станций ... дождитесь завершения"; SendXMLSTS(); } #endregion commitpiket #region commitstation if (reader.Name == "commitstation") { textBoxLog.Text += "COMMIT STATIONS"; textBlockStat.Text = "Данные о станциях переданы"; } #endregion commitstation #region delallwppktbf if (reader.Name == "delallwppktbf") { textBoxLog.Text += "Готов к приему"; SendXML(); } #endregion delallwppktbf #region commitcave if (reader.Name == "commitcave") { textBoxLog.Text += "COMMIT cave"; textBlockStat.Text = "Создана пещера"; refreshCave(); } #endregion commitcave #region commitsession if (reader.Name == "commitsession") { textBoxLog.Text += "commit session"; textBlockStat.Text = "Создана сессия"; refreshCave(); } #endregion commitsession #region pktlist else if (reader.Name == "pktlist") { node = "pktlist"; textBoxLog.Text += "XML node = " + node + "\r\n"; listPk = new List<PiketListItem>(); } #region pkt else if (reader.Name == "pkt" && node == "pktlist") { nodecv = "pkt"; //<pkt id="17" sessionID="2" p_FROM="10" p_TO="11" p_AZIMUT="156,00" p_INC="32,00" p_LENGTH="8,20" //p_LEFT="2,30" p_UP="2,95" p_DOWN="0,00" p_RIGHT="1,97" p_sIsPiket="1" p_sIsPiketWall="1">Ступенька 0.9</pkt> itemPk = new PiketListItem(); itemPk.ID = Convert.ToInt32(reader.GetAttribute("id")); itemPk.FROM = reader.GetAttribute("p_FROM"); itemPk.TO = reader.GetAttribute("p_TO"); itemPk.BEARING = reader.GetAttribute("p_AZIMUT"); itemPk.INC = reader.GetAttribute("p_INC"); itemPk.LENGTH = reader.GetAttribute("p_LENGTH"); itemPk.LEFT = reader.GetAttribute("p_LEFT"); itemPk.UP = reader.GetAttribute("p_UP"); itemPk.DOWN = reader.GetAttribute("p_DOWN"); itemPk.RIGHT = reader.GetAttribute("p_RIGHT"); itemPk.IsPiket = false; if (reader.GetAttribute("p_sIsPiket") == "1") itemPk.IsPiket = true; itemPk.IsPiketWall = false; if (reader.GetAttribute("p_sIsPiketWall") == "1") itemPk.IsPiketWall = true; listPk.Add(itemPk); } #endregion pkt #endregion pktlist } #endregion NodeType == Element #region NodeType == EndElement else if (reader.NodeType == XmlNodeType.EndElement) { if (reader.Name == "cave") { nodecv = ""; } if (reader.Name == "caves") { node = ""; } if (reader.Name == "cavedesc") { nodecv2 = ""; } if (reader.Name == "info") { node = ""; } if (reader.Name == "sessiondesc") { nodecv3 = ""; } if (reader.Name == "pktlist") { node = ""; } if (reader.Name == "pkt") { nodecv = ""; } } #endregion NodeType == EndElement #region NodeType == Text else if (reader.NodeType == XmlNodeType.Text) { if (node == "info") { info = reader.Value; textBoxLog.Text += "XML info = " + info + "\r\n"; } if (nodecv2 == "cavedesc" && node == "caves" && nodecv == "cave") { cavelist[cvint].cavedesc = reader.Value; textBoxLog.Text += "XML cavedesc = " + cavelist[cvint].cavedesc + "\r\n"; } if (nodecv3 == "sessiondesc" && node == "caves") { cavelist[cvint].sessions[sscint].desc = reader.Value; textBoxLog.Text += "XML sessiondesc = " + cavelist[cvint].sessions[sscint].desc + "\r\n"; } if (nodecv == "pkt" && node == "pktlist") { listPk[listPk.Count - 1].COMMENTS = reader.Value; textBoxLog.Text += "XML pktComment = " + listPk[listPk.Count - 1].COMMENTS; } } #endregion NodeType == Text }//while (reader.Read())//Чтение XML #endregion Чтение XML textBoxLog.Text += "cavelist.Count = " + cavelist.Count.ToString() + "\r\n";
Комментариев нет:
Отправить комментарий