Beispiel #1
0
        private decimal GenerateDecimal(DecimalElement decimalElement)
        {
            var randDouble = GenerateDouble(new DoubleElement
            {
                Type        = "Double",
                MinValue    = (double)decimalElement.MinValue,
                MaxValue    = (double)decimalElement.MaxValue,
                ExcludeZero = decimalElement.ExcludeZero
            });

            return((decimal)randDouble);
        }
        private Element ConvertXElementToElement(XElement element)
        {
            var elementValues = new List <string>();

            if (!element.Elements().Any())
            {
                var tElem = _oldRoot.Descendants(element.Name).Where(x => GetParentsAsString(x, -1) == GetParentsAsString(element, -1) && !x.Elements().Any()).Select(e => e.Value);
                elementValues.AddRange(tElem);
            }

            var xElementList = _oldRoot.Descendants(element.Name).GroupBy(el => el.Parent).Select(g => new { g.Key, Count = g.Count() }).Where(x => x.Count > 1);

            Element returnElement;
            var     elementName = element.Name.LocalName;
            var     elementType = DataType.GetDataTypeFromList(elementValues, _dateFormat, _dateTimeFormat);

            switch (elementType.type)
            {
            case DataType.Type.Date:
                returnElement = new DateTimeElement(elementName, _dateFormat);
                break;

            case DataType.Type.DateTime:
                returnElement = new DateTimeElement(elementName, _dateTimeFormat);
                break;

            case DataType.Type.Bool:
                returnElement = new BoolElement(elementName, "True", "False");
                break;

            case DataType.Type.@bool:
                returnElement = new BoolElement(elementName, "true", "false");
                break;

            case DataType.Type.@int:
                returnElement = new IntElement(elementName);
                break;

            case DataType.Type.@decimal:
                returnElement = new DecimalElement(elementName);
                break;

            case DataType.Type.@string:
                returnElement = new StringElement(elementName);
                break;

            default:
                returnElement = new Element(elementName);
                break;
            }
            returnElement.Enumerable      = xElementList.Any();
            returnElement.Type            = elementType;
            returnElement.OriginalElement = element;

            foreach (var xElement in element.Elements())
            {
                returnElement.Elements.Add(ConvertXElementToElement(xElement));
            }

            foreach (var xAttribute in element.Attributes())
            {
                var tElements = _oldRoot.DescendantsAndSelf(element.Name).Where(x => GetParentsAsString(x, -1) == GetParentsAsString(element, -1)).ToList();

                var xAttr           = xAttribute;
                var attributeValues = tElements.Select(tElement => tElement.Attribute(xAttr.Name)).Select(attribute => attribute != null ? attribute.Value : "").ToList();

                Attribute thisAttribute;
                var       attributeName = xAttribute.Name.LocalName;

                if (xAttribute.IsNamespaceDeclaration)
                {
                    returnElement.NamespaceAttributes.Add(xAttribute);
                    continue;
                }

                if (attributeName == "schemaLocation")
                {
                    thisAttribute = new SchemaLocationAttribute(attributeName, xAttribute.Value);
                    returnElement.Attributes.Add(thisAttribute);
                    continue;
                }

                var attributeType = DataType.GetDataTypeFromList(attributeValues, _dateFormat, _dateTimeFormat);
                switch (attributeType.type)
                {
                case DataType.Type.Date:
                    thisAttribute = new DateTimeAttribute(attributeName, _dateFormat);
                    break;

                case DataType.Type.DateTime:
                    thisAttribute = new DateTimeAttribute(attributeName, _dateTimeFormat);
                    break;

                case DataType.Type.Bool:
                    thisAttribute = new BoolAttribute(attributeName, "True", "False");
                    break;

                case DataType.Type.@bool:
                    thisAttribute = new BoolAttribute(attributeName, "true", "false");
                    break;

                case DataType.Type.@int:
                    thisAttribute = new IntAttribute(attributeName);
                    break;

                case DataType.Type.@decimal:
                    thisAttribute = new DecimalAttribute(attributeName);
                    break;

                case DataType.Type.@string:
                    thisAttribute = new StringAttribute(attributeName);
                    break;

                default:
                    thisAttribute = new Attribute(attributeName);
                    break;
                }
                thisAttribute.Type = attributeType;

                returnElement.Attributes.Add(thisAttribute);
            }

            return(returnElement);
        }
        private void ConsolidateElements(Element newElement, Element currentElement)
        {
            // compare current element elements with new element elements and add unique missing to new element
            foreach (var cElement in currentElement.Elements)
            {
                var tempElement = newElement.Elements.FirstOrDefault(e => e.Name == cElement.Name);

                if (tempElement == null)                 // element missing, add it
                {
                    var elementName = cElement.Name;
                    switch (cElement.Type.type)
                    {
                    case DataType.Type.Date:
                        tempElement = new DateTimeElement(elementName, _dateFormat);
                        break;

                    case DataType.Type.DateTime:
                        tempElement = new DateTimeElement(elementName, _dateTimeFormat);
                        break;

                    case DataType.Type.Bool:
                        tempElement = new BoolElement(elementName, "True", "False");
                        break;

                    case DataType.Type.@bool:
                        tempElement = new BoolElement(elementName, "true", "false");
                        break;

                    case DataType.Type.@int:
                        tempElement = new IntElement(elementName);
                        break;

                    case DataType.Type.@decimal:
                        tempElement = new DecimalElement(elementName);
                        break;

                    case DataType.Type.@string:
                        tempElement = new StringElement(elementName);
                        break;

                    default:
                        tempElement = new Element(elementName);
                        break;
                    }

                    tempElement.Enumerable          = cElement.Enumerable;
                    tempElement.Type                = cElement.Type;
                    tempElement.OriginalElement     = cElement.OriginalElement;
                    tempElement.NamespaceAttributes = cElement.NamespaceAttributes;

                    newElement.Elements.Add(tempElement);
                }

                foreach (var attribute in cElement.Attributes)
                {
                    // Check Attribute Exists
                    if (tempElement.Attributes.Any(a => a.Name == attribute.Name))
                    {
                        continue;
                    }

                    var sameAttributes = cElement.Attributes.Where(a => a.Name == attribute.Name).ToList();
                    var dataType       = sameAttributes.Aggregate <Attribute, DataType>(null, (current, sameAttribute) => current == null ? sameAttribute.Type : DataType.GetBestType(current, sameAttribute.Type));
                    attribute.Type = dataType;
                    tempElement.Attributes.Add(attribute);
                }

                ConsolidateElements(tempElement, cElement);
            }
        }