object IDataContractSurrogate.GetCustomDataToExport(Type clrType, Type dataContractType) { if (dataContractType.IsGenericType && dataContractType.GetGenericTypeDefinition() == typeof(Nullable <>)) { dataContractType = dataContractType.GetGenericArguments()[0]; } XmlDocument commentsDoc = XmlCommentsUtils.LoadXmlComments(dataContractType); if (commentsDoc != null) { if (dataContractType.IsEnum) { EnumAnnotation annotation = new EnumAnnotation(); string comment = XmlCommentsUtils.GetFormattedComment(commentsDoc, dataContractType, format); if (comment != null) { annotation.EnumText = comment; } Dictionary <string, MemberInfo> enumMembers = ReflectionUtils.GetEnumMembers(dataContractType); foreach (KeyValuePair <string, MemberInfo> member in enumMembers) { comment = XmlCommentsUtils.GetFormattedComment(commentsDoc, member.Value, format); if (comment != null) { annotation.Members.Add(member.Key, comment); } } if (annotation.EnumText != null || annotation.Members.Count > 0) { return(annotation); } } else { string comment = XmlCommentsUtils.GetFormattedComment(commentsDoc, dataContractType, format); if (comment != null) { return(new Annotation(comment)); } } } return(null); }
private static void ConvertObjectAnnotation(XmlSchemaObject schemaObj) { XmlSchemaAnnotated annObj = schemaObj as XmlSchemaAnnotated; if (annObj != null && annObj.Annotation != null) { XmlSchemaDocumentation comment = null; foreach (XmlSchemaObject annotation in annObj.Annotation.Items) { XmlSchemaAppInfo appInfo = annotation as XmlSchemaAppInfo; if (appInfo != null) { for (int i = 0; i < appInfo.Markup.Length; i++) { XmlNode markup = appInfo.Markup[i]; if (markup != null) { XmlAttribute typeAttrib = markup.Attributes["type", "http://www.w3.org/2001/XMLSchema-instance"]; if (typeAttrib != null) { if (typeAttrib.Value.Contains(":Annotation")) { string ns = typeAttrib.Value.Split(':')[0]; typeAttrib = markup.Attributes[ns, "http://www.w3.org/2000/xmlns/"]; if (typeAttrib != null && typeAttrib.Value == Annotation.AnnotationNamespace) { //This is an annotation created by us. Convert it to ws:documentation element and break comment = CreateDocumentationItem(markup.InnerText); appInfo.Markup[i] = null; break; } } else if (typeAttrib.Value.Contains(":EnumAnnotation")) { string ns = typeAttrib.Value.Split(':')[0]; typeAttrib = markup.Attributes[ns, "http://www.w3.org/2000/xmlns/"]; if (typeAttrib != null && typeAttrib.Value == Annotation.AnnotationNamespace) { DataContractSerializer serializer = new DataContractSerializer(typeof(EnumAnnotation)); using (XmlReader reader = new XmlNodeReader(markup)) { EnumAnnotation enumAnn = (EnumAnnotation)serializer.ReadObject(reader, false); if (enumAnn.EnumText != null) { //This is an annotation created by us. Convert it to ws:documentation element and break comment = CreateDocumentationItem(enumAnn.EnumText); } if (enumAnn.Members.Count > 0) { foreach (XmlSchemaEnumerationFacet enumObj in GetEnumItems(schemaObj)) { string docText; if (enumAnn.Members.TryGetValue(enumObj.Value, out docText)) { if (enumObj.Annotation == null) { enumObj.Annotation = new XmlSchemaAnnotation(); } enumObj.Annotation.Items.Add(CreateDocumentationItem(docText)); } } } appInfo.Markup[i] = null; break; } } } } } } } } if (comment != null) { annObj.Annotation.Items.Add(comment); } } foreach (XmlSchemaObject subObj in GetSubItems(schemaObj)) { ConvertObjectAnnotation(subObj); } }