Подтвердить что ты не робот

Как программно генерировать xml-схему из типа?

Я пытаюсь создать xs: schema из любого .net Тип программно. Я знаю, что я мог бы использовать отражение и генерировать его, повторяя публичные свойства, но есть ли встроенный способ?

Пример:

[Serializable]
public class Person
{
    [XmlElement(IsNullable = false)] public string FirstName { get; set; }
    [XmlElement(IsNullable = false)] public string LastName { get; set; }
    [XmlElement(IsNullable = true)] public string PhoneNo { get; set; }
}

Требуемый выход:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Person" type="Person" />
  <xs:complexType name="Person">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" form="unqualified" name="FirstName" type="xs:string" />
      <xs:element minOccurs="0" maxOccurs="1" form="unqualified" name="LastName" type="xs:string" />
      <xs:element minOccurs="0" maxOccurs="1" form="unqualified" name="PhoneNo" type="xs:string" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>
4b9b3361

Ответ 1

Итак, это работает, я думаю, это было не так уродливо, как казалось:

var soapReflectionImporter = new SoapReflectionImporter();
var xmlTypeMapping = soapReflectionImporter.ImportTypeMapping(typeof(Person));
var xmlSchemas = new XmlSchemas();
var xmlSchema = new XmlSchema();
xmlSchemas.Add(xmlSchema);
var xmlSchemaExporter = new XmlSchemaExporter(xmlSchemas);
xmlSchemaExporter.ExportTypeMapping(xmlTypeMapping);

Я все еще надеялся, что есть 2-строчное решение, похоже, должно быть, спасибо за подсказку @dtb


ИЗМЕНИТЬ Просто для пинков, здесь 2-строчная версия (самоуничижительный юмор)

var typeMapping = new SoapReflectionImporter().ImportTypeMapping(typeof(Person));
new XmlSchemaExporter(new XmlSchemas { new XmlSchema() }).ExportTypeMapping(typeMapping);

Ответ 2

Я обнаружил, что принятый ответ создал неверную схему с некоторыми моими атрибутами. например Он игнорировал пользовательские имена для значений перечисления, отмеченных [XmlEnum(Name="Foo")]

Я считаю, что это правильный путь (с учетом использования XmlSerializer) и довольно просто:

var schemas = new XmlSchemas();
var exporter = new XmlSchemaExporter(schemas);
var mapping = new XmlReflectionImporter().ImportTypeMapping(typeof(Person));
exporter.ExportTypeMapping(mapping);
var schemaWriter = new StringWriter();
foreach (XmlSchema schema in schemas)
{
    schema.Write(schemaWriter);
}
return schemaWriter.ToString();

Код, извлеченный из: http://blogs.msdn.com/b/youssefm/archive/2010/05/13/using-xml-schema-import-and-export-for-xmlserializer.aspx

Ответ 3

Вы можете программно вызывать xsd.exe:

  • Добавьте xsd.exe в качестве ссылки на сборку.
  • using XsdTool;
  • Xsd.Main(new[] { "myassembly.dll", "/type:MyNamespace.MyClass" });

Вы также можете использовать Reflector для поиска метода XsdTool.Xsd.ExportSchemas. Он использует общедоступный XmlReflectionImporter, XmlSchemas, XmlSchema XmlSchemaExporter и XmlTypeMapping для создания схемы из типов .NET.

По существу он делает это:

var importer = new XmlReflectionImporter();
var schemas = new XmlSchemas();
var exporter = new XmlSchemaExporter(schemas);

var xmlTypeMapping = importer.ImportTypeMapping(typeof(Person));
exporter.ExportTypeMapping(xmlTypeMapping);

schemas.Compile(..., false);

for (var i = 0; i < schemas.Count; i++)
{
    var schema = schemas[i];
    schema.Write(...);
}                 ↑

Вы должны иметь возможность настраивать вывод, передавая подходящую запись методу XmlSchema.Write.

Ответ 4

Я считаю, что это то, что вы ищете: Написание собственного XSD.exe

Заимствование кода сверху:

using System;
using System.IO;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.CodeDom;
using System.CodeDom.Compiler;

using Microsoft.CSharp;

using NUnit.Framework;

namespace XmlSchemaImporterTest
{
  [TestFixture]
  public class XsdToClassTests
  {
      // Test for XmlSchemaImporter
      [Test]
      public void XsdToClassTest()
      {
          // identify the path to the xsd
          string xsdFileName = "Account.xsd";
          string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
          string xsdPath = Path.Combine(path, xsdFileName);

          // load the xsd
          XmlSchema xsd;
          using(FileStream stream = new FileStream(xsdPath, FileMode.Open, FileAccess.Read))
          {
              xsd = XmlSchema.Read(stream, null);
          }
          Console.WriteLine("xsd.IsCompiled {0}", xsd.IsCompiled);

          XmlSchemas xsds = new XmlSchemas();
          xsds.Add(xsd);
          xsds.Compile(null, true);
          XmlSchemaImporter schemaImporter = new XmlSchemaImporter(xsds);

          // create the codedom
          CodeNamespace codeNamespace = new CodeNamespace("Generated");
          XmlCodeExporter codeExporter = new XmlCodeExporter(codeNamespace);

          List maps = new List();
          foreach(XmlSchemaType schemaType in xsd.SchemaTypes.Values)
          {
              maps.Add(schemaImporter.ImportSchemaType(schemaType.QualifiedName));
          }
          foreach(XmlSchemaElement schemaElement in xsd.Elements.Values)
          {
              maps.Add(schemaImporter.ImportTypeMapping(schemaElement.QualifiedName));
          }
          foreach(XmlTypeMapping map in maps)
          {
              codeExporter.ExportTypeMapping(map);
          }

          RemoveAttributes(codeNamespace);

          // Check for invalid characters in identifiers
          CodeGenerator.ValidateIdentifiers(codeNamespace);

          // output the C# code
          CSharpCodeProvider codeProvider = new CSharpCodeProvider();

          using(StringWriter writer = new StringWriter())
          {
              codeProvider.GenerateCodeFromNamespace(codeNamespace, writer, new CodeGeneratorOptions());
              Console.WriteLine(writer.GetStringBuilder().ToString());
          }

          Console.ReadLine();
      }

      // Remove all the attributes from each type in the CodeNamespace, except
      // System.Xml.Serialization.XmlTypeAttribute
      private void RemoveAttributes(CodeNamespace codeNamespace)
      {
          foreach(CodeTypeDeclaration codeType in codeNamespace.Types)
          {
              CodeAttributeDeclaration xmlTypeAttribute = null;
              foreach(CodeAttributeDeclaration codeAttribute in codeType.CustomAttributes)
              {
                  Console.WriteLine(codeAttribute.Name);
                  if(codeAttribute.Name == "System.Xml.Serialization.XmlTypeAttribute")
                  {
                      xmlTypeAttribute = codeAttribute;
                  }
              }
              codeType.CustomAttributes.Clear();
              if(xmlTypeAttribute != null)
              {
                  codeType.CustomAttributes.Add(xmlTypeAttribute);
              }
          }
      }
  }
}

Ответ 5

Средство определения схемы XML генерирует XML-схемы или общие классы времени исполнения из файлов XDR, XML и XSD или из классов в сборке времени выполнения.

http://msdn.microsoft.com/en-us/library/x6c1kb0s(VS.71).aspx