private static XmlDocument Serialize(IGraphable graph) { using (var ms = new MemoryStream()) using (var xmlWriter = XmlWriter.Create(ms, new XmlWriterSettings { Indent = true })) using (var xsw = new XmlStateWriter(xmlWriter)) { var structureFormatter = CreateStructureFormater(); xsw.WriteStartElement("ClinicalDocument", "urn:hl7-org:v3"); xsw.WriteAttributeString("xmlns", "xsi", null, XmlIts1Formatter.NS_XSI); xsw.WriteAttributeString("xmlns", null, null, "urn:hl7-org:v3"); xsw.WriteAttributeString("xmlns", "custom", null, CustomNamespace); xsw.WriteAttributeString("xsi", "type", XmlIts1Formatter.NS_XSI, "custom:ClinicalDocument"); structureFormatter.Graph(xsw, graph); xsw.Close(); ms.Seek(0, SeekOrigin.Begin); Debug.WriteLine(Encoding.UTF8.GetString(ms.ToArray())); using (var reader = new XmlTextReader(ms)) { var result = new XmlDocument(); result.Load(reader); return result; } } }
public void EV_1110_DefaultUniprocessorCustomTypeTest() { // Load a sample because I don't want to write a full construction method ClinicalDocument clinicalDocument = null; using (Stream inStream = typeof(EV_1110).Assembly.GetManifestResourceStream("MARC.Everest.Test.Resources.phrDocTesting_140219112155-0500.xml")) { XmlIts1Formatter fmtr = new XmlIts1Formatter(); fmtr.ValidateConformance = false; fmtr.GraphAides.Add(new ClinicalDocumentDatatypeFormatter()); var result = fmtr.Parse(XmlReader.Create(inStream), typeof(ClinicalDocument)); clinicalDocument = result.Structure as ClinicalDocument; } var observation = new ObservationMyProfile() { MoodCode = x_ActMoodDocumentObservation.Eventoccurrence, Code = "3202-20", EntryRelationship = new List<EntryRelationship>() { new EntryRelationship(x_ActRelationshipEntryRelationship.HasComponent, true) { ClinicalStatement = new ObservationMyProfile() } } } ; CascadeNullFlavor(observation.EntryRelationship[0], NullFlavor.Unknown); // Cascade a null flavor on one of the entries clinicalDocument.Component.GetBodyChoiceIfStructuredBody().Component[1].Section.Entry.Add(new Entry( x_ActRelationshipEntry.HasComponent, false, observation )); // Cascade a null flavor on one of the entries clinicalDocument.Component.GetBodyChoiceIfStructuredBody().Component[2].Section.Entry.Add(new Entry( x_ActRelationshipEntry.HasComponent, false, observation )); // Cascade a null flavor on one of the entries clinicalDocument.Component.GetBodyChoiceIfStructuredBody().Component[3].Section.Entry.Add(new Entry( x_ActRelationshipEntry.HasComponent, false, observation )); StringWriter sw = new StringWriter(); using (XmlWriter xw = XmlWriter.Create(sw, new XmlWriterSettings() { Indent = true })) { XmlIts1Formatter fmtr = new XmlIts1Formatter(); fmtr.ValidateConformance = false; fmtr.GraphAides.Add(new ClinicalDocumentDatatypeFormatter()); fmtr.Settings = SettingsType.DefaultUniprocessor; fmtr.Settings |= SettingsType.SuppressXsiNil; fmtr.Settings |= SettingsType.SuppressNullEnforcement; fmtr.Settings |= SettingsType.AlwaysCheckForOverrides; fmtr.RegisterXSITypeName("POCD_MT000040.Observation", typeof(MARC.Everest.Test.Regressions.EV_1102.ObservationWithConfidentialityCode)); using (XmlStateWriter xsw = new XmlStateWriter(xw)) { xsw.WriteStartElement("hl7", "ClinicalDocument", "urn:hl7-org:v3"); xsw.WriteAttributeString("xmlns", "xsi", null, XmlIts1Formatter.NS_XSI); xsw.WriteAttributeString("schemaLocation", XmlIts1Formatter.NS_XSI, @"urn:hl7-org:v3 Schemas/CDA-PITO-E2E.xsd"); xsw.WriteAttributeString("xmlns", null, null, @"urn:hl7-org:v3"); xsw.WriteAttributeString("xmlns", "hl7", null, @"urn:hl7-org:v3"); xsw.WriteAttributeString("xmlns", "e2e", null, @"http://standards.pito.bc.ca/E2E-DTC/cda"); xsw.WriteAttributeString("xmlns", "xs", null, @"http://www.w3.org/2001/XMLSchema"); IFormatterGraphResult result = fmtr.Graph(xsw, clinicalDocument); foreach (ResultDetail itm in result.Details) Trace.WriteLine(String.Format("{0}:{1} @ {2}", itm.Type, itm.Message, itm.Location)); xsw.WriteEndElement(); // clinical document xsw.Flush(); } } Trace.WriteLine(sw.ToString()); Regex re = new Regex(@"\<entryRelationship.*/\>"); if (re.IsMatch(sw.ToString())) Assert.Fail("Output of entry relationship is not as expected"); }
/// <summary> /// Graphs object <paramref name="o"/> onto stream <paramref name="s"/> /// </summary> /// <param name="s">The stream to which <paramref name="o"/> is to be graphed</param> /// <param name="o">The object to be graphed</param> /// <returns>A <see cref="T:MARC.Everest.Formatters.XML.ITS1.Its1FormatterGraphResult"/> structure contianing the /// results of formatting</returns> /// <seealso cref="F:Graph(System.Xml.XmlWriter,MARC.Everest.Interfaces.IGraphable)"/> public IFormatterGraphResult Graph(Stream s, IGraphable o) { ThrowIfDisposed(); XmlWriter xwtr = XmlWriter.Create(s); XmlStateWriter xw = new XmlStateWriter(xwtr); if (o == null) return new XmlIts1FormatterGraphResult(ResultCode.AcceptedNonConformant, null); //TODO: Length will never be less than 0 so the logic should be > 0 instead of != 0. bool needsRoot = o.GetType().GetCustomAttributes(typeof(InteractionAttribute), true).Length == 0; if (needsRoot) { object[] sa = o.GetType().GetCustomAttributes(typeof(StructureAttribute), true); needsRoot = sa.Length == 0 || !(sa[0] as StructureAttribute).IsEntryPoint; if (needsRoot) { xw.WriteStartElement(o.GetType().FullName, "urn:hl7-org:v3"); xw.WriteAttributeString("xmlns", "xsi", null, XmlIts1Formatter.NS_XSI); } } // Determine if this is not an interaction var result = Graph(xw, o); if (needsRoot) xw.WriteEndElement(); xw.Flush(); return result; }