問(wèn)題描述
我正在使用 DataContractSerializer 將我的類(lèi)序列化/反序列化到 XML.一切正常,但在某些時(shí)候,我想為這些 XML 文件的格式建立一個(gè)獨(dú)立于實(shí)際代碼的標(biāo)準(zhǔn)模式.這樣,如果序列化過(guò)程中出現(xiàn)問(wèn)題,我總是可以返回并檢查標(biāo)準(zhǔn)模式應(yīng)該是什么.或者,如果我確實(shí)需要修改架構(gòu),那么修改是一個(gè)明確的決定,而不僅僅是修改我的代碼的后續(xù)影響.
I'm using DataContractSerializer to serialize/deserialize my classes to/from XML. Everything works fine, but at some point I'd like to establish a standard schema for the format of these XML files independent of the actual code. That way if something breaks in the serialization process I can always go back and check what the standard schema should be. Or if I do need to modify the schema the modification is an explicit decision rather then just a later affect of modifying my code.
此外,其他人可能正在編寫(xiě)其他可能不是基于 .NET 的軟件,這些軟件需要從這些 XML 文件中讀取.我希望能夠?yàn)樗麄兲峁┠撤N架構(gòu)文檔.
In addition, other people may be writing other software that may not be .NET based that would need to read from these XML files. I'd like to be able to provide them with some kind of documentation of the schema.
DataContract 和 XSD 架構(gòu)之間是否存在某種關(guān)系.有沒(méi)有辦法將類(lèi)中的 DataContract 屬性導(dǎo)出為 XSD 架構(gòu)?
Is there some relationship between a DataContract and an XSD schema. Is there a way to export the DataContract attributes in classes as an XSD schema?
推薦答案
Svcutil.exe 可以導(dǎo)出已編譯數(shù)據(jù)合約的元數(shù)據(jù)".
DataContract 和 XSD 之間存在關(guān)系:
當(dāng)使用元數(shù)據(jù)終結(jié)點(diǎn)或 ServiceModel 元數(shù)據(jù)實(shí)用工具 (Svcutil.exe) 從 Windows Communication Foundation (WCF) 服務(wù)導(dǎo)出元數(shù)據(jù)時(shí),DataContractSerializer 會(huì)將 CLR 類(lèi)型映射到 XSD.有關(guān)詳細(xì)信息,請(qǐng)參閱數(shù)據(jù)協(xié)定序列化程序.
The DataContractSerializer maps CLR types to XSD when metadata is exported from a Windows Communication Foundation (WCF) service using a metadata endpoint or the ServiceModel Metadata Utility Tool (Svcutil.exe). For more information, see Data Contract Serializer.
當(dāng)使用 Svcutil.exe 訪問(wèn) Web 服務(wù)描述語(yǔ)言 (WSDL) 或 XSD 文檔并為服務(wù)或客戶(hù)端生成數(shù)據(jù)協(xié)定時(shí),DataContractSerializer 還將 XSD 映射到 CLR 類(lèi)型.
The DataContractSerializer also maps XSD to CLR types when Svcutil.exe is used to access Web Services Description Language (WSDL) or XSD documents and generate data contracts for services or clients.
即使在瀏覽器中,您也可以通過(guò) 設(shè)置 MEX 端點(diǎn).
默認(rèn)情況下,WSDL 將包含對(duì)可以通過(guò)端點(diǎn)訪問(wèn)的 XSD 的引用.
You can get the XSD(s) at run-time as well, even in your browser, by setting up a MEX endpoint.
The WSDL by default will contain references to XSD(s) that can be accessed through the endpoint as well.
這篇關(guān)于有沒(méi)有辦法從 DataContract 導(dǎo)出 XSD 架構(gòu)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!