/// <summary> /// Creates an XML representations of a CultureString /// </summary> /// <param name="cultureString"> /// this CultureString /// </param> /// <param name="elementName"> /// Name of the XElement /// </param> /// <param name="wrapInCData"> /// Wrap the contents in CDATA, default = false /// </param> /// <returns> /// XElement with language strings /// </returns> public static XElement CreateCultreStringXElement(this CultureString cultureString, string elementName, bool wrapInCData) { XElement element = new XElement(elementName); foreach (int lang in Enum.GetValues(typeof(Language))) { string str = cultureString.Get(lang); if (string.IsNullOrEmpty(str)) { continue; } XElement content = new XElement("content", wrapInCData ? new XCData(str).ToString() : str); content.AddAttribute("lang", lang); element.Add(content); } return(element); }
private void BuildModelRecursive(XElement elementComplexType, Dictionary <string, string> classes, string trail) { if (elementComplexType != null && elementComplexType.Element(XDocName.Sequence) != null) { List <string> propertyNamesUsed = new List <string>(); var propertyBuilder = new StringBuilder(); foreach (XElement refElement in elementComplexType.Element(XDocName.Sequence).Elements()) { string refName = refElement.AttributeValue("ref"); string classShortRefName = refName.Split('-')[0]; string className = refName.Replace("-", string.Empty); XElement element = GetXElementByNameAttribute(refName); XElement nextComplexType = element.Element(XDocName.ComplexType); if (nextComplexType != null && nextComplexType.Element(XDocName.Attribute) != null) { string groupId = nextComplexType.Element(XDocName.Attribute).AttributeValue("fixed"); ////propertyBuilder.AppendLine("public int GruppeId {get {return " + groupId +";} }"); } // string minOccStr = refElement.AttributeValue("minOccurs"); // int minOccurs = string.IsNullOrEmpty(minOccStr) ? 1 : int.Parse(minOccStr); // writer.AppendLine("public int MinOccurs {get {return " + groupId + ";} }"); //// string maxOccursStr = refElement.AttributeValue("maxOccurs"); int maxOccurs = string.IsNullOrEmpty(maxOccursStr) ? 1 : int.Parse(maxOccursStr); // Prefer annotation from ref element XElement annotationElement = refElement.Element(XDocName.Annotation) ?? element.Element(XDocName.Annotation); if (annotationElement != null) { CultureString labels = CreateCultureStringFromXElement(annotationElement, "LEDE"); if (labels.Count > 0) { propertyBuilder.AppendLine(); propertyBuilder.AppendLine(" /// <summary>" + labels.Get(1044).Replace("\n", string.Empty) + "</summary>"); //\n is messing with my generated code } } string propertyDataType = className; XElement simpleTypeAnnotationElement = null; if (nextComplexType != null && nextComplexType.Element(XDocName.SimpleContent) != null) { XElement extension = nextComplexType.Element(XDocName.SimpleContent).Element(XDocName.Extension); string simpleTypeName = extension.AttributeValue("base"); XElement simpleType = xsd.Descendants(XDocName.SimpleType).FirstOrDefault(p => XmlToLinqExtensions.AttributeValue(p, "name") == simpleTypeName); simpleTypeAnnotationElement = simpleType.Element(XDocName.Annotation); XElement restriction = simpleType.Element(XDocName.Restriction); // int orid = int.Parse(extension.Element(XDocName.Attribute).AttributeValue("fixed")); // writer.WritePropertyName("orid"); // writer.WriteValue(orid); //// string xsdDataType = restriction.AttributeValue("base").ToLower(); switch (xsdDataType) { case "xs:string": case "xs:normalizedstring": propertyDataType = "string"; break; case "xs:int": propertyDataType = "int"; break; case "xs:short": propertyDataType = "short"; break; case "xs:decimal": case "xs:integer": case "xs:negativeinteger": case "xs:positiveinteger": case "xs:nonnegativeinteger": case "xs:nonpositiveinteger": propertyDataType = "decimal"; break; case "xs:date": case "xs:datetime": case "xs:gday": case "xs:gmonthday": case "xs:gyear": case "xs:gyearmonth": case "xs:month": case "xs:time": case "xs:timeperiod": propertyDataType = "DateTime"; break; case "xs:boolean": propertyDataType = "bool"; break; case "xs:double": propertyDataType = "double"; break; case "xs:long": propertyDataType = "long"; break; } // string length = restriction.Element(XDocName.Length).AttributeValue("value"); // if (!string.IsNullOrEmpty(length)) // { // writer.WritePropertyName("length"); // writer.WriteValue(int.Parse(length)); // } //// string minLength = restriction.Element(XDocName.MinLength).AttributeValue("value"); if (!string.IsNullOrEmpty(minLength)) { propertyBuilder.AppendLine(" [MinLength(" + minLength + ")]"); } string maxLength = restriction.Element(XDocName.MaxLength).AttributeValue("value"); if (!string.IsNullOrEmpty(maxLength)) { propertyBuilder.AppendLine(" [MaxLength(" + maxLength + ")]"); } string minInclusive = restriction.Element(XDocName.MinInclusive).AttributeValue("value"); string maxInclusive = restriction.Element(XDocName.MaxInclusive).AttributeValue("value"); if (!string.IsNullOrEmpty(minInclusive) && !string.IsNullOrEmpty(maxInclusive)) { propertyBuilder.AppendLine(" [Range(" + minInclusive + ", " + maxInclusive + ")]"); } // string totalDigits = restriction.Element(XDocName.TotalDigits).AttributeValue("value"); // if (!string.IsNullOrEmpty(totalDigits)) // { // writer.WritePropertyName("totalDigits"); // writer.WriteValue(int.Parse(totalDigits)); // } //// string pattern = restriction.Element(XDocName.Pattern).AttributeValue("value"); if (!string.IsNullOrEmpty(pattern)) { propertyBuilder.AppendLine(" [RegularExpression(@\"" + pattern + "\")]"); } // IEnumerable<XElement> enumerations = restriction.Elements(XDocName.Enumeration); // if (enumerations != null && enumerations.Count() > 0) // { // writer.WritePropertyName("enums"); // writer.WriteStartArray(); // foreach (var enumeration in enumerations) // { // writer.WriteValue(enumeration.AttributeValue("value")); // } // writer.WriteEndArray(); // } } // handle "bad" xsd with duplicate names in same group if (propertyNamesUsed.Contains(classShortRefName)) { classShortRefName += "2"; } if (maxOccurs > 1) { propertyDataType = "List<" + propertyDataType + ">"; } propertyBuilder.AppendLine(" [XmlElement(\"" + refName + "\")]"); propertyBuilder.AppendLine(" public " + propertyDataType + " " + classShortRefName + " {get; set;}"); propertyNamesUsed.Add(classShortRefName); // Magic BuildModelRecursive(nextComplexType, classes, className); } // Add class with properties classes.Add(trail, CreateClass(trail, propertyBuilder.ToString())); } }