Vistual Studio自帶的xsd.exe工具,根據XML自動生成XSD
利用Vistual Studio自帶的xsd.exe工具,根據XML自動生成XSD
1, 命令提示符--》找到vs自帶的xsd.exe工具所在的文件夾 例如: C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin 注意:win7操作系統“命令提示符”要以管理員身份運行
2,將Xml文件拷貝到xsd.exe工具所在的文件夾,生成的xsd文件也將在這個文件夾中
3,在命令提示符中輸入 xsd.exe test.xml, 多個xml文件以空格隔開
===============================================
xsd.exe語法示例
一般情況下,xsd.exe位於C:/Program Files/Microsoft Visual Studio 8/SDK/v2.0/Bin
1、將xsd文件自成.CS類庫。
正確寫法: XSD.EXE xxx.xsd /l:c# /n:namespace
xsd.exe /d /l:C# a.xsd /n:Namespace1.Namespace2
/d 指令指示該工具生成 DataSet,
/l: 告訴該工具要使用哪種語言(例如 C# 或 Visual Basic .NET)。可選的
/n: 指令指示該工具另外為 DataSet 生成名為 XSDSchema.Namespace 的命名空間。該命令的輸出為 XSDSchemaFileName.cs
2、 csc.exe /t:library XSDSchemaFileName.cs /r:System.dll /r:System.Data.dll /t: 指令指示該工具編譯成庫,
/r: 指令指定進行編譯所需的依賴庫。該命令的輸出為 XSDSchemaFileName.dll,它可以在使用
/r: 指令編譯 ADO.NET 應用程序時傳遞到編譯器
一、 如何將.xsn文件轉成.cs文件。
用infopath打開.xsn文件,在文件-另存為源碼,保存后,將會有一系列的文件,將myschema.xsd文件和xsd.exe文件放在同一目錄下,在DOS窗口上運行:
xsd.exe /d /l:C# myschema.xsd /n:Namespace1.Namespace2
就會生成一個myschema.cs文件,部分代碼如下:
//------------------------------------------------------------------------------
// <auto-generated>
// 此代碼由工具生成。
// 運行庫版本:2.0.50727.42
//
// 對此文件的更改可能會導致不正確的行為,並且如果
// 重新生成代碼,這些更改將會丟失。
// </auto-generated>
//------------------------------------------------------------------------------
//
// This source code was auto-generated by xsd, Version=2.0.50727.42.
//
namespace Namespace1.Namespace2 {
using System;
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "2.0.0.0")]
[Serializable()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.ComponentModel.ToolboxItem(true)]
[System.Xml.Serialization.XmlSchemaProviderAttribute("GetTypedDataSetSchema")]
[System.Xml.Serialization.XmlRootAttribute("NewDataSet")]
[System.ComponentModel.Design.HelpKeywordAttribute("vs.data.DataSet")]
public partial class NewDataSet : System.Data.DataSet {
。。。。。。。。。。。
二、Xsd
xsd.exe myschema.xsd /c /o:"c:/infopath"
或者xsd.exe myschema.xsd /c /o:
XmlSchema 類
按照 WWW 聯合會 (W3C) XML 架構第 1 部分:“結構”和 XML 架構第 2 部分:“數據類型規范”內容指定的 XML 架構內存中表示形式。
命名空間:System.Xml.Schema 程序集:System.Xml(在 system.xml.dll 中)
![]() |
---|
使用 XmlSchema 類會引發異常,如 XmlSchemaException 類可能包含不應在不可信方案中公開的敏感信息。例如,XmlSchemaException 的 SourceUri 屬性返回的架構文件的 URI 路徑導致異常。SourceUri 屬性不應在不受信任的情況下公開。應該正確處理異常以便此敏感信息不會在不受信任的情況下公開。 |
下面的示例創建了一個架構定義。
using System; using System.Xml; using System.Xml.Schema; class XMLSchemaExamples { public static void Main() { XmlSchema schema = new XmlSchema(); // <xs:element name="cat" type="xs:string"/> XmlSchemaElement elementCat = new XmlSchemaElement(); schema.Items.Add(elementCat); elementCat.Name = "cat"; elementCat.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); // <xs:element name="dog" type="xs:string"/> XmlSchemaElement elementDog = new XmlSchemaElement(); schema.Items.Add(elementDog); elementDog.Name = "dog"; elementDog.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); // <xs:element name="redDog" substitutionGroup="dog" /> XmlSchemaElement elementRedDog = new XmlSchemaElement(); schema.Items.Add(elementRedDog); elementRedDog.Name = "redDog"; elementRedDog.SubstitutionGroup = new XmlQualifiedName("dog"); // <xs:element name="brownDog" substitutionGroup ="dog" /> XmlSchemaElement elementBrownDog = new XmlSchemaElement(); schema.Items.Add(elementBrownDog); elementBrownDog.Name = "brownDog"; elementBrownDog.SubstitutionGroup = new XmlQualifiedName("dog"); // <xs:element name="pets"> XmlSchemaElement elementPets = new XmlSchemaElement(); schema.Items.Add(elementPets); elementPets.Name = "pets"; // <xs:complexType> XmlSchemaComplexType complexType = new XmlSchemaComplexType(); elementPets.SchemaType = complexType; // <xs:choice minOccurs="0" maxOccurs="unbounded"> XmlSchemaChoice choice = new XmlSchemaChoice(); complexType.Particle = choice; choice.MinOccurs = 0; choice.MaxOccursString = "unbounded"; // <xs:element ref="cat"/> XmlSchemaElement catRef = new XmlSchemaElement(); choice.Items.Add(catRef); catRef.RefName = new XmlQualifiedName("cat"); // <xs:element ref="dog"/> XmlSchemaElement dogRef = new XmlSchemaElement(); choice.Items.Add(dogRef); dogRef.RefName = new XmlQualifiedName("dog"); XmlSchemaSet schemaSet = new XmlSchemaSet(); schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackOne); schemaSet.Add(schema); schemaSet.Compile(); XmlSchema compiledSchema = null; foreach (XmlSchema schema1 in schemaSet.Schemas()) { compiledSchema = schema1; } XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable()); nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema"); compiledSchema.Write(Console.Out, nsmgr); } public static void ValidationCallbackOne(object sender, ValidationEventArgs args) { Console.WriteLine(args.Message); } }
下面的 XML 文件是為前面的代碼示例生成的。
<?xml version="1.0" encoding="IBM437"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="cat" type="xs:string"/> <xs:element name="dog" type="xs:string"/> <xs:element name="redDog" type="xs:string" substitutionGroup="dog"/> <xs:element name="brownDog" type="xs:string" substitutionGroup ="dog" /> <xs:element name="pets"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element ref="cat"/> <xs:element ref="dog"/> </xs:choice> </xs:complexType> </xs:element> </xs:schema>