private static void DoHit(string element, string value, Hit curHit) { switch (element) { case "Hit_num": // ignore break; case "Hit_id": curHit.Id = value; break; case "Hit_def": curHit.Def = value; break; case "Hit_accession": curHit.Accession = value; break; case "Hit_len": curHit.Length = long.Parse(value, CultureInfo.InvariantCulture); break; case "Hit_hsps": // ignore break; default: string message = String.Format( CultureInfo.CurrentCulture, Properties.Resource.UnknownElement, "DoHit", element); Trace.Report(message); throw new FormatException(message); } }
/// <summary> /// This method expects a single XML document and returns one BlastResult. /// </summary> /// <param name="doc">A Stringbuilder containing the XML document.</param> /// <returns>The BlastResult.</returns> private static BlastResult ParseXML(StringBuilder doc) { BlastResult result = new BlastResult(); try { XmlReaderSettings settings = new XmlReaderSettings(); settings.DtdProcessing = DtdProcessing.Ignore; // don't error when encountering a DTD spec // Setting the XmlResolver to null causes the DTDs specified in the XML // header to be ignored. settings.XmlResolver = null; StringReader sr = null; try { sr = new StringReader(doc.ToString()); using (XmlReader r = XmlReader.Create(sr, settings)) { string curElement = string.Empty; BlastSearchRecord curRecord = new BlastSearchRecord(); Hit curHit = null; Hsp curHsp = null; BlastStatistics curStatistics = null; BlastXmlMetadata curMetadata = null; while (r.Read()) { switch (r.NodeType) { case XmlNodeType.Element: curElement = r.Name; // ApplicationLog.WriteLine("element: " + curElement); if (curElement == "Hit") { curHit = new Hit(); } else if (curElement == "Hsp") { curHsp = new Hsp(); } else if (curElement == "Statistics") { curStatistics = new BlastStatistics(); } else if (curElement == "BlastOutput") { curMetadata = new BlastXmlMetadata(); } break; case XmlNodeType.Text: // ApplicationLog.WriteLine("text: " + r.Value); if (curElement.StartsWith("BlastOutput_", StringComparison.OrdinalIgnoreCase)) { DoBlastOutput(curElement, r.Value, curMetadata); } else if (curElement.StartsWith("Parameters_", StringComparison.OrdinalIgnoreCase)) { DoParameters(curElement, r.Value, curMetadata); } else if (curElement.StartsWith("Iteration_", StringComparison.OrdinalIgnoreCase)) { DoIteration(curElement, r.Value, curRecord); } else if (curElement.StartsWith("Statistics_", StringComparison.OrdinalIgnoreCase)) { DoStatistics(curElement, r.Value, curStatistics); } else if (curElement.StartsWith("Hit_", StringComparison.OrdinalIgnoreCase)) { DoHit(curElement, r.Value, curHit); } else if (curElement.StartsWith("Hsp_", StringComparison.OrdinalIgnoreCase)) { DoHsp(curElement, r.Value, curHsp); } else { ApplicationLog.WriteLine("BlastXMLParser Unhandled: curElement '{0}'", curElement); } break; case XmlNodeType.XmlDeclaration: // ApplicationLog.WriteLine("declaration: {0}, {1}", r.Name, r.Value); break; case XmlNodeType.ProcessingInstruction: // ApplicationLog.WriteLine("instruction: {0}, {1}", r.Name, r.Value); break; case XmlNodeType.Comment: // ApplicationLog.WriteLine("comment: " + r.Value); break; case XmlNodeType.EndElement: // ApplicationLog.WriteLine("endelement: " + r.Name); if (r.Name == "Iteration") { result.Records.Add(curRecord); curRecord = new BlastSearchRecord(); } else if (r.Name == "Statistics") { curRecord.Statistics = curStatistics; } else if (r.Name == "Hit") { curRecord.Hits.Add(curHit); } else if (r.Name == "Hsp") { curHit.Hsps.Add(curHsp); } else if (r.Name == "BlastOutput") { result.Metadata = curMetadata; } break; } } } } finally { if (sr != null) { sr.Dispose(); } } } catch (Exception e) { ApplicationLog.Exception(e); throw; } return(result); }