简体   繁体   English

使用 WCF 反序列化 XML 字符串

[英]Deserialize an XML string with WCF

I have a problem with deserialize an XML string into C# objects.我在将 XML 字符串反序列化为 C# 对象时遇到问题。 The problem is that I the result will always be null.问题是我的结果将永远是 null。 This is my XML string which is stored into a string variable.这是我的 XML 字符串,它存储在字符串变量中。

<objects>
  <object>
    <dateadded>1614924419</dateadded>
    <id>CF7E7B22-1D3A-4CFE-91F9-F8C5C2DB4069</id>
    <name>NorthernLights</name>
    <parentid>ED98C97F-A202-48ED-AEEA-34362508A30B</parentid>
    <type>file</type>
  </object>
  <object>
    <dateadded>1614924419</dateadded>
    <id>CF7E7B22-1D3A-4CFE-91F9-F8C5C2DB4070</id>
    <name>Northern</name>
    <parentid>ED98C97F-A202-48ED-AEEA-34362508A30C</parentid>
    <type>file</type>
  </object>
</objects>

Those are the classes that I have created that represent the XML document.这些是我创建的代表 XML 文档的类。 I suppose something is wrong there.我想那里有问题。 Can someone help me with this?有人可以帮我弄这个吗?

This is the class for the objects tag:这是objects标签的 class:

namespace DataContractSample.DataContract.Model
{
    using System.Collections.Generic;
    using System.Runtime.Serialization;

    [DataContract(Name = "objects", Namespace = "", IsReference = false)]
    [KnownType(typeof(IList<Image>))]

    public class Images
    {
        [DataMember(Name = "object", EmitDefaultValue = false, IsRequired = true)]
        public IList<Image> ImageList;
    }
}

This is the class for the object tag:这是object标签的 class:

namespace DataContractSample.DataContract.Model
{
    using System;
    using System.Runtime.Serialization;

    [DataContract(Name = "object", Namespace = "")]
    public class Image
    {
        [DataMember(Name = "Id")]
        public Guid id { get; set; }

        [DataMember(Name="parentid")]
        public Guid ParentId { get; set; }

        [DataMember(Name="name")]
        public string Name { get; set; }
        
        [DataMember(Name = "type")]
        public string Type { get; set; }

        [DataMember(Name = "dateadded")]
        public string DateAdded { get; set; }
    }
}

Thanks!谢谢!

The problem with your approach is that you want to deserialize the <objects> into a class and into a property at the same time.您的方法的问题是您想同时将<objects>反序列化为class属性 This won't work.这行不通。 Here, I present two different ways how you can solve this problem.在这里,我提出了两种不同的方法来解决这个问题。

Option A选项 A

If you have an outer node around <objects> :如果您在<objects>周围有一个外部节点:

<?xml version="1.0" encoding="utf-8" ?>
<top>
  <objects>
    <object>
      <dateadded>1614924419</dateadded>
      <id>CF7E7B22-1D3A-4CFE-91F9-F8C5C2DB4069</id>
      <name>NorthernLights</name>
      <parentid>ED98C97F-A202-48ED-AEEA-34362508A30B</parentid>
      <type>file</type>
    </object>
    <object>
      <dateadded>1614924419</dateadded>
      <id>CF7E7B22-1D3A-4CFE-91F9-F8C5C2DB4070</id>
      <name>Northern</name>
      <parentid>ED98C97F-A202-48ED-AEEA-34362508A30C</parentid>
      <type>file</type>
    </object>
  </objects>
</top>

with this modified top level entity:使用此修改后的顶级实体:

[DataContract(Name = "top", Namespace = "", IsReference = false)]
public class Images
{
    [DataMember(Name = "objects", EmitDefaultValue = false, IsRequired = true)]
    public List<Image> ImageList;
}

then deserialization works like a charm:然后反序列化就像一个魅力:

using var file = File.OpenRead("sample.xml");
using var reader = new XmlTextReader(file); 
var serializer = new DataContractSerializer(typeof(Images));
var data = (Images)serializer.ReadObject(reader);

Console.WriteLine(data.ImageList.Count); // Will print 2

Option B选项 B

If you don't have an outer node around <objects> then you can use CollectionDataContract :如果<objects>周围没有外部节点,则可以使用CollectionDataContract

[CollectionDataContract(Name = "objects", Namespace = "")]
public class Images: List<Image>
{

}

and it will work as well:它也会起作用:

using var file = File.OpenRead("sample.xml");
using var reader = new XmlTextReader(file); 
var serializer = new DataContractSerializer(typeof(Images));
var data = (Images)serializer.ReadObject(reader);

Console.WriteLine(data.Count); // Will print 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM