2017-02-16 100 views
-2

存在一個叫做XSD(XML Schema Definition)的標準,它產生一個XML格式的類定義。生成的類可以使用以下語言:VB, C#, JS, VJS, C++如何從XSD/XML自動生成特定.NET語言的類的代碼?

如何實現從此XML到特定語言的重新創建類?

的XML文件的例子可以如下:

<?xml version="1.0" encoding="utf-8" ?> 
<MyFile VersionProduct="7.0" VersionCreate="7.00" VersionFile="7.00"> 
    <MyTable Size="20"> 
    <StandardTable Type="TXT"> 
     <MyRecord Priority="1" Size="0" Length="350" Weight="270" Age="40" Ref="80"/> 
     <MyRecord Priority="100" Size="0" Length="27080" Weight="27000" Age="40" Ref="80"/> 
    </StandardTable> 
    </MyTable> 
    <MyTable Size="28"> 
    <StandardTable Type="TXT"> 
     <MyRecord Priority="1" Size="0" Length="350" Weight="270" Age="40" Ref="80"/> 
     <MyRecord Priority="100" Size="0" Length="27080" Weight="27000" Age="40" Ref="80"/> 
    </StandardTable> 
    <StandardTable Type="TRT"> 
     <MyRecord Priority="100" Size="0" Length="980" Weight="900" Age="40" Ref="0"/> 
    </StandardTable> 
    </MyTable> 
</MyFile> 

***有有更多的內容可以XML文件,我想從自動生成代碼,這個XML只是用來提供一個簡單的例子。

+1

你試過了什麼?任何代碼示例? 'EnvDTE'&'VSIX'是你正在尋找的關鍵字。 – Tatranskymedved

+0

@Tatranskymedved @Tatranskymedved我已經找到了解決方案,我只是認爲我會分享,將它作爲答案張貼在 –

+0

以下如果你倒退了,請解釋一下,我會很樂意改變我的問題 –

回答

1

如果您想要解析XML文件並在.net中創建自動生成的類,那麼您應該使用xsd.exe工具來完成此工作,該工具是Visual Studio中開發工具的一部分。

您將需要打開Dev Command Prompt來運行該工具。 Open Command Prompt from inside Visual Studio

VS 2013命令提示符的默認位置是here,但是這可能會根據安裝位置而改變。

在命令提示符下運行以下命令:

XSD 'yourfilename'.xml

例子: enter image description here 這將自動生成的XML計劃文件' yourfilename'.xsd在目錄文件夾中。

接下來,我們要使用自動創建的XSD文件在我們所選擇的語言自動生成的代碼:

XSD「yourfilename'.xsd /班/語言:VB

示例: enter image description here 這將從XSD文件創建一個自動生成的.NET類,該類將包含在名爲'yourfilename'.vb的文件中。

然後,您可以簡單地將您的類文件添加到您的項目。我選擇VB,但你可以選擇'CS','VB','JS','VJS'或'CPP'。

這是創建在VB文件中的代碼:

'------------------------------------------------------------------------------ 
' <auto-generated> 
'  This code was generated by a tool. 
'  Runtime Version:4.0.30319.42000 
' 
'  Changes to this file may cause incorrect behavior and will be lost if 
'  the code is regenerated. 
' </auto-generated> 
'------------------------------------------------------------------------------ 

Option Strict Off 
Option Explicit On 

Imports System.Xml.Serialization 

' 
'This source code was auto-generated by xsd, Version=4.0.30319.33440. 
' 

'''<remarks/> 
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440"), _ 
System.SerializableAttribute(), _ 
System.Diagnostics.DebuggerStepThroughAttribute(), _ 
System.ComponentModel.DesignerCategoryAttribute("code"), _ 
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true), _ 
System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=false)> _ 
Partial Public Class MyFile 

    Private myTableField() As MyFileMyTable 

    Private versionProductField As String 

    Private versionCreateField As String 

    Private versionFileField As String 

    '''<remarks/> 
    <System.Xml.Serialization.XmlElementAttribute("MyTable", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _ 
    Public Property MyTable() As MyFileMyTable() 
     Get 
      Return Me.myTableField 
     End Get 
     Set 
      Me.myTableField = value 
     End Set 
    End Property 

    '''<remarks/> 
    <System.Xml.Serialization.XmlAttributeAttribute()> _ 
    Public Property VersionProduct() As String 
     Get 
      Return Me.versionProductField 
     End Get 
     Set 
      Me.versionProductField = value 
     End Set 
    End Property 

    '''<remarks/> 
    <System.Xml.Serialization.XmlAttributeAttribute()> _ 
    Public Property VersionCreate() As String 
     Get 
      Return Me.versionCreateField 
     End Get 
     Set 
      Me.versionCreateField = value 
     End Set 
    End Property 

    '''<remarks/> 
    <System.Xml.Serialization.XmlAttributeAttribute()> _ 
    Public Property VersionFile() As String 
     Get 
      Return Me.versionFileField 
     End Get 
     Set 
      Me.versionFileField = value 
     End Set 
    End Property 
End Class 

'''<remarks/> 
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440"), _ 
System.SerializableAttribute(), _ 
System.Diagnostics.DebuggerStepThroughAttribute(), _ 
System.ComponentModel.DesignerCategoryAttribute("code"), _ 
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true)> _ 
Partial Public Class MyFileMyTable 

    Private standardTableField() As MyFileMyTableStandardTable 

    Private sizeField As String 

    '''<remarks/> 
    <System.Xml.Serialization.XmlElementAttribute("StandardTable", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _ 
    Public Property StandardTable() As MyFileMyTableStandardTable() 
     Get 
      Return Me.standardTableField 
     End Get 
     Set 
      Me.standardTableField = value 
     End Set 
    End Property 

    '''<remarks/> 
    <System.Xml.Serialization.XmlAttributeAttribute()> _ 
    Public Property Size() As String 
     Get 
      Return Me.sizeField 
     End Get 
     Set 
      Me.sizeField = value 
     End Set 
    End Property 
End Class 

'''<remarks/> 
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440"), _ 
System.SerializableAttribute(), _ 
System.Diagnostics.DebuggerStepThroughAttribute(), _ 
System.ComponentModel.DesignerCategoryAttribute("code"), _ 
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true)> _ 
Partial Public Class MyFileMyTableStandardTable 

    Private myRecordField() As MyFileMyTableStandardTableMyRecord 

    Private typeField As String 

    '''<remarks/> 
    <System.Xml.Serialization.XmlElementAttribute("MyRecord", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _ 
    Public Property MyRecord() As MyFileMyTableStandardTableMyRecord() 
     Get 
      Return Me.myRecordField 
     End Get 
     Set 
      Me.myRecordField = value 
     End Set 
    End Property 

    '''<remarks/> 
    <System.Xml.Serialization.XmlAttributeAttribute()> _ 
    Public Property Type() As String 
     Get 
      Return Me.typeField 
     End Get 
     Set 
      Me.typeField = value 
     End Set 
    End Property 
End Class 

'''<remarks/> 
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440"), _ 
System.SerializableAttribute(), _ 
System.Diagnostics.DebuggerStepThroughAttribute(), _ 
System.ComponentModel.DesignerCategoryAttribute("code"), _ 
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true)> _ 
Partial Public Class MyFileMyTableStandardTableMyRecord 

    Private priorityField As String 

    Private sizeField As String 

    Private lengthField As String 

    Private weightField As String 

    Private ageField As String 

    Private refField As String 

    '''<remarks/> 
    <System.Xml.Serialization.XmlAttributeAttribute()> _ 
    Public Property Priority() As String 
     Get 
      Return Me.priorityField 
     End Get 
     Set 
      Me.priorityField = value 
     End Set 
    End Property 

    '''<remarks/> 
    <System.Xml.Serialization.XmlAttributeAttribute()> _ 
    Public Property Size() As String 
     Get 
      Return Me.sizeField 
     End Get 
     Set 
      Me.sizeField = value 
     End Set 
    End Property 

    '''<remarks/> 
    <System.Xml.Serialization.XmlAttributeAttribute()> _ 
    Public Property Length() As String 
     Get 
      Return Me.lengthField 
     End Get 
     Set 
      Me.lengthField = value 
     End Set 
    End Property 

    '''<remarks/> 
    <System.Xml.Serialization.XmlAttributeAttribute()> _ 
    Public Property Weight() As String 
     Get 
      Return Me.weightField 
     End Get 
     Set 
      Me.weightField = value 
     End Set 
    End Property 

    '''<remarks/> 
    <System.Xml.Serialization.XmlAttributeAttribute()> _ 
    Public Property Age() As String 
     Get 
      Return Me.ageField 
     End Get 
     Set 
      Me.ageField = value 
     End Set 
    End Property 

    '''<remarks/> 
    <System.Xml.Serialization.XmlAttributeAttribute()> _ 
    Public Property Ref() As String 
     Get 
      Return Me.refField 
     End Get 
     Set 
      Me.refField = value 
     End Set 
    End Property 
End Class 

'''<remarks/> 
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440"), _ 
System.SerializableAttribute(), _ 
System.Diagnostics.DebuggerStepThroughAttribute(), _ 
System.ComponentModel.DesignerCategoryAttribute("code"), _ 
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true), _ 
System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=false)> _ 
Partial Public Class NewDataSet 

    Private itemsField() As MyFile 

    '''<remarks/> 
    <System.Xml.Serialization.XmlElementAttribute("MyFile")> _ 
    Public Property Items() As MyFile() 
     Get 
      Return Me.itemsField 
     End Get 
     Set 
      Me.itemsField = value 
     End Set 
    End Property 
End Class 

我跑了XSD。再次EXE工具來生成C#類以及

enter image description here

其次爲這是自genearted代碼:

//------------------------------------------------------------------------------ 
    // <auto-generated> 
    //  This code was generated by a tool. 
    //  Runtime Version:4.0.30319.42000 
    // 
    //  Changes to this file may cause incorrect behavior and will be lost if 
    //  the code is regenerated. 
    // </auto-generated> 
    //------------------------------------------------------------------------------ 

    using System.Xml.Serialization; 

    // 
    // This source code was auto-generated by xsd, Version=4.0.30319.33440. 
    // 


    /// <remarks/> 

    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")] 
    [System.SerializableAttribute()] 
    [System.Diagnostics.DebuggerStepThroughAttribute()] 
    [System.ComponentModel.DesignerCategoryAttribute("code")] 
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] 
    [System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)] 
    public partial class MyFile { 


    private MyFileMyTable[] myTableField; 

    private string versionProductField; 

    private string versionCreateField; 

    private string versionFileField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("MyTable", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] 
    public MyFileMyTable[] MyTable { 
     get { 
      return this.myTableField; 
     } 
     set { 
      this.myTableField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string VersionProduct { 
     get { 
      return this.versionProductField; 
     } 
     set { 
      this.versionProductField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string VersionCreate { 
     get { 
      return this.versionCreateField; 
     } 
     set { 
      this.versionCreateField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string VersionFile { 
     get { 
      return this.versionFileField; 
     } 
     set { 
      this.versionFileField = value; 
     } 
    } 


} 

    /// <remarks/> 
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")] 
    [System.SerializableAttribute()] 
    [System.Diagnostics.DebuggerStepThroughAttribute()] 
    [System.ComponentModel.DesignerCategoryAttribute("code")] 
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] 
    public partial class MyFileMyTable { 

    private MyFileMyTableStandardTable[] standardTableField; 

    private string sizeField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("StandardTable", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] 
    public MyFileMyTableStandardTable[] StandardTable { 
     get { 
      return this.standardTableField; 
     } 
     set { 
      this.standardTableField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Size { 
     get { 
      return this.sizeField; 
     } 
     set { 
      this.sizeField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] 
public partial class MyFileMyTableStandardTable { 

    private MyFileMyTableStandardTableMyRecord[] myRecordField; 

    private string typeField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("MyRecord", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] 
    public MyFileMyTableStandardTableMyRecord[] MyRecord { 
     get { 
      return this.myRecordField; 
     } 
     set { 
      this.myRecordField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Type { 
     get { 
      return this.typeField; 
     } 
     set { 
      this.typeField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] 
public partial class MyFileMyTableStandardTableMyRecord { 

    private string priorityField; 

    private string sizeField; 

    private string lengthField; 

    private string weightField; 

    private string ageField; 

    private string refField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Priority { 
     get { 
      return this.priorityField; 
     } 
     set { 
      this.priorityField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Size { 
     get { 
      return this.sizeField; 
     } 
     set { 
      this.sizeField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Length { 
     get { 
      return this.lengthField; 
     } 
     set { 
      this.lengthField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Weight { 
     get { 
      return this.weightField; 
     } 
     set { 
      this.weightField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Age { 
     get { 
      return this.ageField; 
     } 
     set { 
      this.ageField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Ref { 
     get { 
      return this.refField; 
     } 
     set { 
      this.refField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] 
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)] 
public partial class NewDataSet { 

    private MyFile[] itemsField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("MyFile")] 
    public MyFile[] Items { 
     get { 
      return this.itemsField; 
     } 
     set { 
      this.itemsField = value; 
     } 
    } 
} 

**你也可以提供命名空間作爲參數。

+3

問題和答案都不會提供線索,最終會發生什麼。問題是:問題太短,範圍廣而不清晰(您可以提供xml示例,生成的類等來清楚地顯示*問題*),答案看起來像是兩個非常簡單的步驟的組合(告訴我哪一個更困難):閱讀有關xsd工具的文檔,並從VS中搜索如何使用它。 – Sinatr

+0

@Sinatr我添加了一個XML示例以及該工具生成的文件內容。我希望這可以讓你更容易理解問題和答案 –