本文介绍了使用XDocument作为XmlSerializer.Deserialize的源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调用XmlSerializer.Deserialize并将其传递给XDocument.可能需要StreamXmlReaderTextReader.

我可以在不将XDocument实际转储到某些中间存储区(例如MemoryStream)的情况下,从XDocument生成以上内容之一吗?

似乎我所追求的是与XDocument一起工作的XmlReader的实现.不过我找不到.

解决方案

您可以使用 XDocument.CreateReader() 创建一个读取XDocument内容的XmlReader.

等效地,以下内容也将起作用.

XmlReader GetReader(XDocument doc)
{
    return doc.Root.CreateReader();
}

I would like to invoke XmlSerializer.Deserialize passing it an XDocument. It can take a Stream, an XmlReader or a TextReader.

Can I generate one of the above from XDocument without actually dumping the XDocument into some intermediate store, such as a MemoryStream?

It seems that what I'm after is an implementation of XmlReader that works with an XDocument. I can't find one though.

解决方案

You can use XDocument.CreateReader() to create an XmlReader that reads the contents of the XDocument.

Equivalently, the following will work too.

XmlReader GetReader(XDocument doc)
{
    return doc.Root.CreateReader();
}

这篇关于使用XDocument作为XmlSerializer.Deserialize的源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 19:35