xmlNode 到对象

发布于 2024-08-07 14:42:06 字数 152 浏览 6 评论 0原文

我一直在使用基于 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 技术交流群。

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

发布评论

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

评论(3

少女的英雄梦 2024-08-14 14:42:06

如果您安装了 WCF Rest Starter Kit 预览版,那么有一个巧妙的技巧:

  • 打开 Visual Studio
  • 选择 XML 节点内容(构成节点之一的 XML),然后
  • 从 Visual Studio 中的“编辑”菜单 将其复制到剪贴板,选择“将 XML 作为类型粘贴”

这会将剪贴板上的 XML 作为能够反序列化该确切 XML 的 C# 类粘贴到项目中。相当漂亮!

请参阅有关它的以下博客文章:

这应该会节省你大量的打字时间,让生活变得更美好容易多了!

更新:
好的,您已经根据返回的 XML 生成了类。现在您需要将 XmlNode 转换为您的类。

您必须执行以下操作:

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;
}

您需要将 XmlNode 的 XML 表示形式(属性 .OuterXml)写入流(此处为 MemoryStream< /code>),然后使用 XmlSerializer 从该流序列化回对象。

您可以使用通用方法并调用来完成此操作

 Customer myCustomer = ConvertNode<Customer>(xmlNode);

,甚至可以将该代码转换为 XmlNode 类上的扩展方法,这样您就可以编写:

 Customer myCustomer = xmlNode.ConvertNode<Customer>();

Marc

If you have the WCF Rest Starter Kit preview installed, there's a neat trick:

  • 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"

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:

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.

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:

 Customer myCustomer = xmlNode.ConvertNode<Customer>();

Marc

心奴独伤 2024-08-14 14:42:06

也许在这里回答已经太晚了,但它会对其他人有所帮助:

这是您可以从 XML 节点反序列化的解决方案。

 XmlDocument xmlDoc = new XmlDocument();
 xmlDoc.LoadXml(xml);

 XmlNode xmlNode = xmlDoc.SelectSingleNode("//SystemInfo");

 XmlSerializer serial = new XmlSerializer(typeof(SystemInfo));
 
 using(XmlNodeReader reader = new XmlNodeReader(xmlNode)) {
     SystemInfo syso =(SystemInfo)serial.Deserialize(reader);
 }

首先将 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.

 XmlDocument xmlDoc = new XmlDocument();
 xmlDoc.LoadXml(xml);

 XmlNode xmlNode = xmlDoc.SelectSingleNode("//SystemInfo");

 XmlSerializer serial = new XmlSerializer(typeof(SystemInfo));
 
 using(XmlNodeReader reader = new XmlNodeReader(xmlNode)) {
     SystemInfo syso =(SystemInfo)serial.Deserialize(reader);
 }

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 with using) to the Deserialize method, you will get the objects populated in the class object just like I populated syso object with XML values.

Happy Coding :)

够运 2024-08-14 14:42:06

最简单的方法是使用 .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.

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