如何使用 DataContractSerializer 从 XMLDocument 的单个节点进行反序列化?

发布于 2024-10-11 15:31:06 字数 722 浏览 2 评论 0原文

DataContractSerializer 在 ReadObject 中使用的读取器似乎是基于流的。我已经有一个 XMLDocument,并且我想反序列化该文档的一部分。

在此代码片段中,我从文件流反序列化 MyItem 对象:

Dim fs As New FileStream("c:\myinputfile.xml", FileMode.Open)
Dim reader As XmlDictionaryReader = XmlDictionaryReader.CreateTextReader(fs, New XmlDictionaryReaderQuotas())
Dim ser As New DataContractSerializer(GetType(MyItemClass))
Dim deserializedMyItem As ClassTemplate = CType(ser.ReadObject(reader, True), MyItemClass)

但是如果我只想反序列化文档的一部分,该怎么办?

Dim MyItemNode as Xml.XmlNode = parentDoc.selectSingleNode("MyItemElementName")
Dim deserializedMyItem As MyItemClass = getMyItemFromNode(MyItemNode)

getMyItemFromNode 是什么样子的?

The readers that DataContractSerializer uses in ReadObject seem to be stream-based. I already have an XMLDocument, and I would like to deserialize one part of that document.

In this code snippet, I am deserializing a MyItem object from a filestream:

Dim fs As New FileStream("c:\myinputfile.xml", FileMode.Open)
Dim reader As XmlDictionaryReader = XmlDictionaryReader.CreateTextReader(fs, New XmlDictionaryReaderQuotas())
Dim ser As New DataContractSerializer(GetType(MyItemClass))
Dim deserializedMyItem As ClassTemplate = CType(ser.ReadObject(reader, True), MyItemClass)

but what if instead I want to deserialize just a part of a document.

Dim MyItemNode as Xml.XmlNode = parentDoc.selectSingleNode("MyItemElementName")
Dim deserializedMyItem As MyItemClass = getMyItemFromNode(MyItemNode)

What does getMyItemFromNode look like?

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

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

发布评论

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

评论(2

无敌元气妹 2024-10-18 15:31:06

除非有更好的方法来流式传输它,否则这是使用内存流的解决方案:

Function getMyItemFromNode(ByVal itemNode as Xml.XmlNode) as MyItemClass
    Dim stm As New MemoryStream()
    Dim stw  As New StreamWriter(stm)
    stw.Write(itemNode.OuterXml)
    stw.Flush()
    stm.Position = 0
    Dim reader As XmlDictionaryReader = XmlDictionaryReader.CreateTextReader(stm, New XmlDictionaryReaderQuotas())
    Dim ser As New DataContractSerializer(GetType(MyItemClass))
    Return CType(ser.ReadObject(reader, True), MyItemClass)

End Function 

将其推送到流的代码来自 此回复相关问题。

Unless there is a better way that streaming it, here's a solution using the memorystream:

Function getMyItemFromNode(ByVal itemNode as Xml.XmlNode) as MyItemClass
    Dim stm As New MemoryStream()
    Dim stw  As New StreamWriter(stm)
    stw.Write(itemNode.OuterXml)
    stw.Flush()
    stm.Position = 0
    Dim reader As XmlDictionaryReader = XmlDictionaryReader.CreateTextReader(stm, New XmlDictionaryReaderQuotas())
    Dim ser As New DataContractSerializer(GetType(MyItemClass))
    Return CType(ser.ReadObject(reader, True), MyItemClass)

End Function 

Code for pushing it to a stream came from this response to a related question.

玉环 2024-10-18 15:31:06

使用 [CreateDictionaryReader](http://msdn.microsoft.com/en-us/library/system.xml.xmldictionaryreader.createdictionaryreader.aspx) 以及指向所需节点的XmlReader`:

Dim doc As XmlDocument = new XmlDocument()
' fill it

Dim nav as XPathNavigator = doc.CreateNavigator()
Dim docReader As XmlReader = nav.ReadSubTree()
Dim reader As XmlDictionaryReader = _
    XmlDictionaryReader.CreateDictionaryReader(docReader)
Dim ser As New DataContractSerializer(GetType(MyItemClass))

Use [CreateDictionaryReader](http://msdn.microsoft.com/en-us/library/system.xml.xmldictionaryreader.createdictionaryreader.aspx) with anXmlReader` pointing to the node you want:

Dim doc As XmlDocument = new XmlDocument()
' fill it

Dim nav as XPathNavigator = doc.CreateNavigator()
Dim docReader As XmlReader = nav.ReadSubTree()
Dim reader As XmlDictionaryReader = _
    XmlDictionaryReader.CreateDictionaryReader(docReader)
Dim ser As New DataContractSerializer(GetType(MyItemClass))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文