如何轻松地将多态类型从 XML 反序列化为 POCO(使用 XML Schema)?
假设我想解析这些 XML 元素中的任何一个:
<Server hostName="foobar" />
<WebServer hostName="foobar" />
<WebServer hostName="foobar" listeningOnPort="80" />
<Server xsi:type="WebServer" hostName="foobar" listeningOnPort="80" />
这些元素遵循以下 XML 架构声明:
<xsd:element name="Server" type="IServer" />
<xsd:element name="WebServer" substitutionGroup="Server" type="IWebServer" />
<xsd:complexType name="IServer">
<xsd:attribute name="hostName" type="xsd:string" use="required" />
</xsd:complexType>
<xsd:complexType name="IWebServer">
<xsd:complexContent>
<xsd:extension base="IServer">
<xsd:attribute name="listeningOnPort" type="xsd:int" use="optional" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
到这些类型的相应 CLR 对象:
class Server : IServer { .. } // [CorrespondsTo("Server")]
class WebServer : Server, IWebServer { .. } // [CorrespondsTo("WebServer")]
interface IServer // [CorrespondsTo("IServer")]
{
string HostName { get; set; } // [CorrespondsTo("hostName")]
}
interface IWebServer : IServer // [CorrespondsTo("IWebServer")]
{
int? ListeningOnPort { get; set; } // [CorrespondsTo("listeningOnPort")]
}
.NET 框架或任何成熟的轻量级 XML 解析库中是否有任何内容可以在不需要我的情况下实现此目的必须编写大量代码吗?拥有 XML 架构实际上有助于 XML 到 CLR 类型的映射吗?
- XML 到 CLR 类型映射的声明性解决方案会很好,例如通过 C# 属性。
- 可选属性应正确转换为可为 null 的类型。
应该生成一个WebServer
对象。
Suppose I want to parse any of these XML elements:
<Server hostName="foobar" />
<WebServer hostName="foobar" />
<WebServer hostName="foobar" listeningOnPort="80" />
<Server xsi:type="WebServer" hostName="foobar" listeningOnPort="80" />
which adhere to the following XML Schema declarations:
<xsd:element name="Server" type="IServer" />
<xsd:element name="WebServer" substitutionGroup="Server" type="IWebServer" />
<xsd:complexType name="IServer">
<xsd:attribute name="hostName" type="xsd:string" use="required" />
</xsd:complexType>
<xsd:complexType name="IWebServer">
<xsd:complexContent>
<xsd:extension base="IServer">
<xsd:attribute name="listeningOnPort" type="xsd:int" use="optional" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
to corresponding CLR objects of these types:
class Server : IServer { .. } // [CorrespondsTo("Server")]
class WebServer : Server, IWebServer { .. } // [CorrespondsTo("WebServer")]
interface IServer // [CorrespondsTo("IServer")]
{
string HostName { get; set; } // [CorrespondsTo("hostName")]
}
interface IWebServer : IServer // [CorrespondsTo("IWebServer")]
{
int? ListeningOnPort { get; set; } // [CorrespondsTo("listeningOnPort")]
}
Is there anything in the .NET framework or any mature, lightweight XML parsing library that will achieve this without me having to write lots of code? Does having an XML Schema actually help with the XML-to-CLR type mapping?
- A declarative solution for XML-to-CLR type mappings would be nice, e.g. via C# attributes.
- Optional attributes should be transformed correctly to nullable types.
<Server xsi:type="WebServer" ... />
should result in aWebServer
object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
.NET 本身支持 XML 到 CLR 序列化。
有一个名为“XSD.exe”的命令行工具,它采用一系列 .xsd 文档并创建相应的 C# 对象。
http://msdn.microsoft.com/en-us/ library/x6c1kb0s(VS.71).aspx
然后在代码中,使用 XmlSerializer 类进行序列化和反序列化。
http://msdn.microsoft.com/en-us /library/system.xml.serialization.xmlserializer.aspx
.NET supports XML to CLR serialization natively.
There is a command line tool called "XSD.exe" which takes a series of .xsd documents and creates the corresponding C# objects.
http://msdn.microsoft.com/en-us/library/x6c1kb0s(VS.71).aspx
Then in code, you use the XmlSerializer class to serialize and deserialize.
http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx