private XElement GetSchema(XsdSchema xsdSchema, string id) { var schema = new XElement( _xs + "schema", new XAttribute("id", id), new XAttribute("targetNamespace", xsdSchema.TargetNamespace), new XAttribute("elementFormDefault", "qualified"), new XAttribute("attributeFormDefault", "unqualified"), new XAttribute(XNamespace.Xmlns + "xs", _xs.ToString()) ); if (!string.IsNullOrEmpty(xsdSchema.XmlNamespace)) { schema.SetAttributeValue("xmlns", xsdSchema.XmlNamespace); } if (!string.IsNullOrEmpty(xsdSchema.Namespace)) { schema.SetAttributeValue(XNamespace.Xmlns + "ns", xsdSchema.Namespace); } if (!string.IsNullOrEmpty(xsdSchema.Common)) { schema.SetAttributeValue(XNamespace.Xmlns + "common", xsdSchema.Common); } return(schema); }
private XsdFile BuildSummary(XsdSchema defaultSchema, XElement[] defaultImports, string packageId) { XsdFile xsdFile = new XsdFile(); xsdFile.Schema = GetSchema(defaultSchema, packageId); foreach (var import in defaultImports) { xsdFile.Imports.Add(import); } //include所有其它自动生成的xsd文件 foreach (var item in _xsdFiles) { xsdFile.Imports.Add(new XElement( _xs + "include", new XAttribute("schemaLocation", item.Key + ".xsd") )); } //添加dll中所有定义的element foreach (var item in _elements) { xsdFile.Elements.Add(item); } return(xsdFile); }
public void Build(Assembly assembly) { _assembly = assembly; _xsdFiles = new Dictionary <string, XsdFile>(); _elements = new List <XElement>(); XElement[] defaultImports = GetDefaultImports(_assembly).ToArray(); XsdSchema defaultSchema = XsdSchema.Get(_assembly); string directoryName = defaultSchema.PackageId ?? _assembly.GetName().Name; if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); } BuildTypes(_assembly); foreach (var item in _xsdFiles) { item.Value.Schema = GetSchema(defaultSchema, item.Key); foreach (var import in defaultImports) { item.Value.Imports.Add(import); } //生成XSD文件 string fileName = string.Format("{0}/{1}.xsd", directoryName, item.Key); item.Value.ToXML().Save(fileName); Console.WriteLine("Generate {0}", fileName); } //生成汇总XSD文件 var summaryXsdFile = BuildSummary(defaultSchema, defaultImports, directoryName); string summaryXsdFileName = string.Format("{0}/{1}.xsd", directoryName, directoryName); summaryXsdFile.ToXML().Save(summaryXsdFileName); Console.WriteLine(string.Format("{0}Generate Summary{0}\n{1}", new String('=', 10), summaryXsdFileName)); }
private XElement GetSchema(XsdSchema xsdSchema, string id) { var schema = new XElement( _xs + "schema", new XAttribute("id", id), new XAttribute("targetNamespace", xsdSchema.TargetNamespace), new XAttribute("elementFormDefault", "qualified"), new XAttribute("attributeFormDefault", "unqualified"), new XAttribute(XNamespace.Xmlns + "xs", _xs.ToString()) ); if (!string.IsNullOrEmpty(xsdSchema.XmlNamespace)) schema.SetAttributeValue("xmlns", xsdSchema.XmlNamespace); if (!string.IsNullOrEmpty(xsdSchema.Namespace)) schema.SetAttributeValue(XNamespace.Xmlns + "ns", xsdSchema.Namespace); if (!string.IsNullOrEmpty(xsdSchema.Common)) schema.SetAttributeValue(XNamespace.Xmlns + "common", xsdSchema.Common); return schema; }
private XsdFile BuildSummary(XsdSchema defaultSchema, XElement[] defaultImports, string packageId) { XsdFile xsdFile = new XsdFile(); xsdFile.Schema = GetSchema(defaultSchema, packageId); foreach (var import in defaultImports) { xsdFile.Imports.Add(import); } //include所有其它自动生成的xsd文件 foreach (var item in _xsdFiles) { xsdFile.Imports.Add(new XElement( _xs + "include", new XAttribute("schemaLocation", item.Key + ".xsd") )); } //添加dll中所有定义的element foreach (var item in _elements) { xsdFile.Elements.Add(item); } return xsdFile; }