private static CompiledDocumentReferences GetCompiledDocumentReferencesFromCompiledDocument(XmlReader xmlReader) { XElement referencesElement = xmlReader.ElementsNamed("references").First(); CompiledDocumentReferences references = CreateCompiledDocumentReferences(referencesElement); return(references); }
public static CompiledDocumentReferences GetCompiledDocumentReferences(string xmlBlob) { // See http://stackoverflow.com/questions/3831676/ca2202-how-to-solve-this-case for an explanation of why // the warning is being suppressed TextReader outerTextReader = new StringReader(xmlBlob); string outerCdata = null; using (var outerXmlReader = new XmlTextReader(outerTextReader)) { while (outerXmlReader.Read()) { if (outerXmlReader.NodeType == XmlNodeType.CDATA) { outerCdata = outerXmlReader.Value; break; } } } if (outerCdata == null) { throw new Exception("Outer CDATA not found"); } using (Stream base64DecodedStream = new Base64DecodingStream(new StringReader(outerCdata))) { using (TextReader compiledDocumentTextReader = new StreamReader(base64DecodedStream, Encoding.Default)) { CompiledDocumentReferences references = GetCompiledDocumentReferencesFromCompiledDocument(compiledDocumentTextReader); return(references); } } }
static void Main(string[] args) { string xmlblob = File.ReadAllText(@"C:\Users\epohl\Dropbox\projects\Base64Streaming\XmlBlob.xml"); CompiledDocumentReferences references = XmlBlobHelper.GetCompiledDocumentReferences(xmlblob); Console.WriteLine("references: categoryId = {0}, parentId = {1}, dapId = {2}", references.CategoryId, references.ParentId, references.DapId); foreach (var doc in references.Documents) { Console.WriteLine("\tDocument: Id = {0}, Guid = {1}, Name = '{2}', SpecialtyName = '{3}', CategoryName = '{4}'", doc.Id, doc.Guid, doc.Name, doc.SpecialtyName, doc.CategoryName); } }
// We could convert the XElement to an XmlElement here and call iMed's CompiledDocumentReferences constructor. // See http://blogs.msdn.com/b/ericwhite/archive/2008/12/22/convert-xelement-to-xmlnode-and-convert-xmlnode-to-xelement.aspx private static CompiledDocumentReferences CreateCompiledDocumentReferences(XElement element) { var references = new CompiledDocumentReferences { CategoryId = (int)element.Attribute("categoryId"), DapId = (int)element.Attribute("dapId"), ParentId = (int)element.Attribute("parentId") }; foreach (XElement documentElement in element.Elements("document")) { references.Documents.Add(CreateCompiledDocumentReferenceDocument(documentElement)); } return(references); }