C# xmlNode 到对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1563473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
xmlNode to objects
提问by 76mel
I have been working with a 3rd party java based REST webservice, that returns an array of xmlNodes.
我一直在使用基于 3rd 方 java 的 REST web 服务,它返回一个 xmlNode 数组。
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 ?
xmlNode[] 代表一个对象,我正在尝试找出反序列化对象中的 xmlNode[] 的最佳方法?是先建立一个 xmlDocument 并反序列化吗?
Thanks
谢谢
采纳答案by marc_s
If you have the WCF Rest Starter Kit preview installed, there's a neat trick:
如果您安装了 WCF Rest Starter Kit 预览版,则有一个巧妙的技巧:
- open Visual Studio
- select your XML node contents (the XML that makes up one of your nodes) and copy it to the clipboard
- from your "Edit" menu in Visual Studio, pick "Paste XML as Types"
- 打开 Visual Studio
- 选择您的 XML 节点内容(构成您的节点之一的 XML)并将其复制到剪贴板
- 从 Visual Studio 的“编辑”菜单中,选择“将 XML 粘贴为类型”
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!
这会将剪贴板上的 XML 作为能够反序列化该确切 XML 的 C# 类粘贴到您的项目中。相当漂亮!
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.
更新:
好的,您已经从返回的 XML 生成了您的类。现在您需要将 a 转换XmlNode
为您的类。
You'll have to do something like this:
你必须做这样的事情:
private static T ConvertNode<T>(XmlNode node) where T: class
{
MemoryStream stm = new MemoryStream();
StreamWriter stw = new StreamWriter(stm);
stw.Write(node.OuterXml);
stw.Flush();
stm.Position = 0;
XmlSerializer ser = new XmlSerializer(typeof(T));
T result = (ser.Deserialize(stm) as T);
return result;
}
You need to write the XML representation (property .OuterXml
) of the XmlNode
to a stream (here a MemoryStream
) and then use the XmlSerializer
to serialize back the object from that stream.
您需要将 的 XML 表示(属性.OuterXml
)写入XmlNode
流(此处为 a MemoryStream
),然后使用XmlSerializer
将对象从该流中序列化回来。
You can do it with the generic method and call
您可以使用泛型方法并调用
Customer myCustomer = ConvertNode<Customer>(xmlNode);
or you could even turn that code into either an extension method on the XmlNode
class so you could write:
或者您甚至可以将该代码转换为XmlNode
类上的扩展方法,以便您可以编写:
Customer myCustomer = xmlNode.ConvertNode<Customer>();
Marc
马克
回答by Jeffrey Lott
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.
最简单的方法是使用 .NET 中内置的 System.Xml.Serialization.XmlSerializer 类。在 XmlSerializer 上的谷歌搜索将提供大量教程,您可以使用它们来查找适合您的教程。
回答by Khawaja Asim
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.
这是您可以Deserialize
从 XML 节点获得的解决方案。
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
XmlNode xmlNode = xmlDoc.SelectSingleNode("//SystemInfo");
XmlSerializer serial = new XmlSerializer(typeof(SystemInfo));
SystemInfo syso =(SystemInfo)serial.Deserialize(new XmlNodeReader(xmlNode));
The first load the XML to XmlDocument Object
and then find the parent node you will wish to deserialize just like I want SystemInfoobject node from all the XML document.
首先将 XML 加载到XmlDocument Object
,然后找到您希望反序列化的父节点,就像我想要来自所有 XML 文档的SystemInfo对象节点一样。
Once you find that create an XmlSerializer
object with the specific class type you will wish to.
一旦你发现XmlSerializer
用你想要的特定类类型创建一个对象。
Now just pass the new XmlNodeReader(xmlNode)
to the Deserializemethod, you will get the objects populated in the class object just like I populated syso
object with XML values.
现在只需将 传递new XmlNodeReader(xmlNode)
给Deserialize方法,您将在类对象中填充syso
对象,就像我用 XML 值填充对象一样。
Happy Coding :)
快乐编码:)