2010-11-10 10 views
8

मैं एक एक्सएमएल एक्सएसडी स्कीमा से एक वर्ग उत्पन्न करना चाहता हूं, जैसा कि आप Xsd.exe tool के साथ कर सकते हैं।प्रोग्रामेटिक रूप से XSD.exe उपकरण सुविधा का उपयोग करें (एक्सएसडी स्कीमा से कक्षा उत्पन्न करें) .NET Framework कक्षाओं के माध्यम से?

उदा। XSD.exe /namespace:Generated.Xsd_1 /classes /outputdir:..\Classes

क्या स्टैंडअलोन टूल का उपयोग करने के बजाय .NET Framework में कक्षाओं का उपयोग करके करने का कोई तरीका है?

+2

@slugster: हाँ, क्योंकि यह एक अलग सवाल है - रिवर्स पहले की स्थिति। एक्सएसडी उपकरण दो तरीकों से जाता है - प्रत्येक के लिए एक अलग समाधान की आवश्यकता होती है। हर किसी के लिए ध्यान दें: http://stackoverflow.com/questions/4150002/ की कोई जानकारी नहीं कृपया ध्यान से पढ़ें! –

+0

mmmmkay, मेरा बुरा। एक त्वरित स्कीम पढ़ने के साथ यह सवाल कथित डुप्ले का एक कट डाउन संस्करण प्रतीत होता है। – slugster

उत्तर

11

बेशर्मी here से उधार लिया:

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); 
       } 
      } 
     } 
    } 
} 
+0

+1 वह वही लेख था जिसकी मैं सोच रहा था! – ewall

संबंधित मुद्दे