/// <summary> /// Initializes a new instance. /// </summary> /// <param name="innerException"></param> public SDataException(WebException innerException) : base(innerException.Message, innerException, innerException.Status, innerException.Response) { if (Response == null) { return; } var httpResponse = Response as HttpWebResponse; _statusCode = httpResponse != null ? httpResponse.StatusCode : (HttpStatusCode?) null; MediaType mediaType; if (MediaTypeNames.TryGetMediaType(Response.ContentType, out mediaType) && mediaType == MediaType.Xml) { var serializer = new XmlSerializer(typeof (Diagnosis)); using (var stream = Response.GetResponseStream()) { try { _diagnosis = (Diagnosis) serializer.Deserialize(stream); } catch (XmlException) { } catch (InvalidOperationException) { } } } }
public void Diagnosis_SDataCode_Should_Be_An_Enum_Test() { var diagnosis = new Diagnosis { SDataCode = DiagnosisCode.ApplicationDiagnosis, ApplicationCode = "Application error" }; string xml; using (var textWriter = new StringWriter()) using (var xmlWriter = new XmlTextWriter(textWriter)) { diagnosis.WriteTo(xmlWriter, null); xml = textWriter.ToString(); } XPathNavigator nav; using (var textReader = new StringReader(xml)) using (var xmlReader = new XmlTextReader(textReader)) { nav = new XPathDocument(xmlReader).CreateNavigator(); } var node = nav.SelectSingleNode("diagnosis/sdataCode"); Assert.IsNotNull(node); Assert.AreEqual("ApplicationDiagnosis", node.Value); node = nav.SelectSingleNode("diagnosis/applicationCode"); Assert.IsNotNull(node); Assert.AreEqual("Application error", node.Value); }
/// <summary> /// Initializes the syndication extension context using the supplied <see cref="XPathNavigator"/>. /// </summary> /// <param name="source">The <b>XPathNavigator</b> used to load this <see cref="SDataExtensionContext"/>.</param> /// <param name="manager">The <see cref="XmlNamespaceManager"/> object used to resolve prefixed syndication extension elements and attributes.</param> /// <returns><b>true</b> if the <see cref="SDataExtensionContext"/> was able to be initialized using the supplied <paramref name="source"/>; otherwise <b>false</b>.</returns> /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="manager"/> is a null reference (Nothing in Visual Basic).</exception> public bool Load(XPathNavigator source, XmlNamespaceManager manager) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ var wasLoaded = false; //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNull(source, "source"); Guard.ArgumentNotNull(manager, "manager"); //------------------------------------------------------------ // Attempt to extract syndication extension information //------------------------------------------------------------ if (source.HasChildren) { var payloadNavigator = source.SelectSingleNode("sdata:payload/*", manager); if (payloadNavigator != null) { var payload = new SDataPayload(); if (payload.Load(payloadNavigator, manager)) { Payload = payload; wasLoaded = true; } } var diagnosesNavigator = source.Select("sdata:diagnoses", manager); foreach (XPathNavigator item in diagnosesNavigator) { var diagnosis = new Diagnosis(); if (diagnosis.Load(item, manager)) { Diagnoses.Add(diagnosis); wasLoaded = true; } } var schemaNavigator = source.SelectSingleNode("sdata:schema", manager); if (schemaNavigator != null && !string.IsNullOrEmpty(schemaNavigator.Value)) { Schema = schemaNavigator.Value; wasLoaded = true; } } return wasLoaded; }