/// <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 { var settings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore }; 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; switch (curElement) { case "Hit": curHit = new Hit(); break; case "Hsp": curHsp = new Hsp(); break; case "Statistics": curStatistics = new BlastStatistics(); break; case "BlastOutput": curMetadata = new BlastXmlMetadata(); break; } break; case XmlNodeType.Text: 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); } break; case XmlNodeType.XmlDeclaration: case XmlNodeType.ProcessingInstruction: case XmlNodeType.Comment: break; case XmlNodeType.EndElement: 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; }
/// <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); }
/// <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 { var settings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore }; 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; switch (curElement) { case "Hit": curHit = new Hit(); break; case "Hsp": curHsp = new Hsp(); break; case "Statistics": curStatistics = new BlastStatistics(); break; case "BlastOutput": curMetadata = new BlastXmlMetadata(); break; } break; case XmlNodeType.Text: 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); } break; case XmlNodeType.XmlDeclaration: case XmlNodeType.ProcessingInstruction: case XmlNodeType.Comment: break; case XmlNodeType.EndElement: 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); }