xmlNode 到对象
我一直在使用基于 Java 的第 3 方 REST Web 服务,它返回 xmlNode 数组。
xmlNode[] 代表一个对象,我正在尝试找出反序列化对象中的 xmlNode[] 的最佳方法?是先建立一个 xmlDocument 然后反序列化吗?
谢谢
I have been working with a 3rd party java based REST webservice, that returns an array of xmlNodes.
The xmlNode[] respresent an object and I am trying to work out the best way to Deserialize the xmlNode[] in the object? is it to build up a xmlDocument first and the Deserialize ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您安装了 WCF Rest Starter Kit 预览版,那么有一个巧妙的技巧:
这会将剪贴板上的 XML 作为能够反序列化该确切 XML 的 C# 类粘贴到项目中。相当漂亮!
请参阅有关它的以下博客文章:
这应该会节省你大量的打字时间,让生活变得更美好容易多了!
更新:
好的,您已经根据返回的 XML 生成了类。现在您需要将
XmlNode
转换为您的类。您必须执行以下操作:
您需要将
XmlNode
的 XML 表示形式(属性.OuterXml
)写入流(此处为MemoryStream< /code>),然后使用
XmlSerializer
从该流序列化回对象。您可以使用通用方法并调用来完成此操作
,甚至可以将该代码转换为
XmlNode
类上的扩展方法,这样您就可以编写:Marc
If you have the WCF Rest Starter Kit preview installed, there's a neat trick:
This will paste your XML that's on the clipboard into your project as a C# class that is capable of deserializing that exact XML. Pretty nifty!
See these blog posts about it:
That should save you a lot of typing and make life a lot easier!
UPDATE:
OK, you already have your classes generated from the XML you get back. Now you need to convert a
XmlNode
to your class.You'll have to do something like this:
You need to write the XML representation (property
.OuterXml
) of theXmlNode
to a stream (here aMemoryStream
) and then use theXmlSerializer
to serialize back the object from that stream.You can do it with the generic method and call
or you could even turn that code into either an extension method on the
XmlNode
class so you could write:Marc
也许在这里回答已经太晚了,但它会对其他人有所帮助:
这是您可以从 XML 节点
反序列化
的解决方案。首先将 XML 加载到
XmlDocument 对象
,然后找到您希望反序列化的父节点,就像我想要来自所有 XML 文档的 SystemInfo 对象节点一样。一旦您发现使用您希望的特定类类型创建一个
XmlSerializer
对象。现在只需将
reader
(使用using
创建)传递给 Deserialize 方法,您将获得填充在类对象中的对象,就像我填充的一样带有 XML 值的 syso 对象。快乐编码:)
Maybe this is too late to answer here but it will help others:
Here is the solution you will be able to
Deserialize
from the XML node.The first load the XML to
XmlDocument Object
and then find the parent node you will wish to deserialize just like I want SystemInfo object node from all the XML document.Once you find that create an
XmlSerializer
object with the specific class type you will wish to.Now just pass the
reader
(created withusing
) to the Deserialize method, you will get the objects populated in the class object just like I populatedsyso
object with XML values.Happy Coding :)
最简单的方法是使用 .NET 中内置的 System.Xml.Serialization.XmlSerializer 类。在 Google 上搜索 XmlSerializer 将提供大量教程,您可以使用它们来查找适合您的教程。
The easiest way to do this would be to use the built in System.Xml.Serialization.XmlSerializer class in .NET. A google search on XmlSerializer will provide a ton of tutorials that you can use to find a tutorial that works for you.