使用 XSLT 进行自定义转换的 XmlSerializer

发布于 2024-08-28 03:19:36 字数 60 浏览 4 评论 0原文

有没有办法通过应用 XSLT 中定义的自定义转换来使用 XmlSerializer 反序列化 XML 流?

Is there a way to deserialize a XML stream using XmlSerializer by applying a custom transform defined in a XSLT?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你与昨日 2024-09-04 03:19:36

我认为没有一个 API 调用可以实现这一点,但您当然可以按照以下方法通过几行代码来实现:

XslCompiledTransform proc = new XslCompiledTransform();
proc.Load("sheet.xsl");

XmlDocument tempResult = new XmlDocument();
using (XmlWriter xw = tempResult.CreateNavigator().AppendChild())
{
  proc.Transform("input.xml", null, xw);
  xw.Close();
}

XmlSerializer ser = new XmlSerializer(typeof(Foo));
Foo foo = (Foo)ser.Deserialize(new XmlNodeReader(tempResult));

I don't think there is a single API call that would allow that but you could certainly implement that with a few lines along the following approach:

XslCompiledTransform proc = new XslCompiledTransform();
proc.Load("sheet.xsl");

XmlDocument tempResult = new XmlDocument();
using (XmlWriter xw = tempResult.CreateNavigator().AppendChild())
{
  proc.Transform("input.xml", null, xw);
  xw.Close();
}

XmlSerializer ser = new XmlSerializer(typeof(Foo));
Foo foo = (Foo)ser.Deserialize(new XmlNodeReader(tempResult));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文