Пример #1
0
        private XElement BuildComplexType(Type type, out string fileGroup)
        {
            var xsdComplexType = XsdComplexType.Get(type);

            //添加XSD文件
            fileGroup = xsdComplexType.FileGroup;
            SetDefaultFile(fileGroup);

            //只有Request或者Response对象类型,末尾自动添加Type
            string name = (type.Name.EndsWith("Request") || type.Name.EndsWith("Response"))
                ? type.Name + "Type"
                : type.Name;

            var complexTypeElement = new XElement(
                _xs + "complexType",
                new XAttribute("name", name)
                );

            if (!string.IsNullOrEmpty(xsdComplexType.Annotation))
            {
                complexTypeElement.Add(new XElement(
                                           _xs + "annotation",
                                           new XElement(_xs + "documentation", xsdComplexType.Annotation)
                                           ));
            }

            var sequenceElement = BuildSequence(type);

            AddProperties(type, sequenceElement);
            complexTypeElement.Add(sequenceElement);
            return(complexTypeElement);
        }
Пример #2
0
        private void AddProperties(Type type, XElement sequenceElement)
        {
            var properties = type.GetProperties();

            foreach (var propertyInfo in properties)
            {
                var typeName        = Common.GetXsdTypeName(propertyInfo.PropertyType);
                var propertyElement = new XElement(
                    _xs + "element",
                    new XAttribute("name", propertyInfo.Name),
                    new XAttribute("type", typeName)
                    );

                var xsdElement = XsdElement.Get(propertyInfo);
                if (xsdElement != null)
                {
                    if (!string.IsNullOrEmpty(xsdElement.MinOccurs))
                    {
                        propertyElement.SetAttributeValue("minOccurs", xsdElement.MinOccurs);
                    }
                    if (!string.IsNullOrEmpty(xsdElement.MaxOccurs))
                    {
                        propertyElement.SetAttributeValue("maxOccurs", xsdElement.MaxOccurs);
                    }

                    if (!string.IsNullOrEmpty(xsdElement.Annotation))
                    {
                        propertyElement.Add(new XElement(
                                                _xs + "annotation",
                                                new XElement(
                                                    _xs + "documentation", xsdElement.Annotation
                                                    )
                                                ));
                    }
                }

                //判断是否自定义类型, 添加Import
                if (!typeName.StartsWith("xs:"))
                {
                    var parentClassFileGroup   = XsdComplexType.Get(type).FileGroup;
                    var propertyClassFileGroup = Common.GetFileGroup(propertyInfo.PropertyType);
                    if (parentClassFileGroup != propertyClassFileGroup)
                    {
                        string importXsd = propertyClassFileGroup + ".xsd";
                        //判断是否已经存在该Import
                        if (_xsdFiles[parentClassFileGroup].Imports.All(item => item.Attribute("schemaLocation").Value != importXsd))
                        {
                            _xsdFiles[parentClassFileGroup].Imports.Add(
                                new XElement(
                                    _xs + "include",
                                    new XAttribute("schemaLocation", importXsd)
                                    )
                                );
                        }
                    }
                }
                sequenceElement.Add(propertyElement);
            }
        }
Пример #3
0
        public static string GetFileGroup(Type type)
        {
            type = GetPropertyType(type);

            if (type.IsClass)
            {
                return(XsdComplexType.Get(type).FileGroup);
            }
            if (type.IsEnum)
            {
                return(XsdSimpleType.Get(type).FileGroup);
            }
            return(null);
        }