c#结构可以序列化为“值类型"吗?还是它的特性之一? [英] Can a c# struct be serialized as a "value type" or just one of its properties?

查看:111
本文介绍了c#结构可以序列化为“值类型"吗?还是它的特性之一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用具有两个属性的结构,是否可以将其序列化为单个属性而不是对象?

Using a struct with two properties, is it possible to serialize it as a single property instead of an object?

    public struct Child
    {
        public string Name { get; set; }
        public string Code { get; set; }
    }

    public class Parent
    {
        public Child Child { get; set; }
    }

代替:

<Parent>
  <Child>
    <Code>PA</Code>
    <Name>Pennsylvania</Name>
  </Child>
</Parent>

...我希望将其反序列化为:

...I'd like it to deserialize as:

<Parent>
  <Child>PA</Child>
</Parent>

并且,还应该与JSON一起使用.提前谢谢!

And, should work with JSON also. Thanks in advance!

推荐答案

tallman5,

Hi tallman5,

感谢您在此处发布.

>>

>>Using a struct with two properties, is it possible to serialize it as a single property instead of an object?

struct和XML是对应的.如果要在代码中序列化该结构,则可以使用以下代码.序列化是根据类的结构进行的,因此无法序列化部分结构.

The struct and XML are corresponding. If you want to serialize the struct in your code, you could use the following code. The serialize is according to the struct of the class, hence it is impossible to serialize part of the struct.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace serlize_objext
{
    class Program
    {
        static void Main(string[] args)
        {
            Parent myObject = new Parent();
            // Insert code to set properties and fields of the object.  
            XmlSerializer mySerializer = new
            XmlSerializer(typeof(Parent));
            // To write to a file, create a StreamWriter object.  
            StreamWriter myWriter = new StreamWriter(@"C:\Users\v-wezan\Desktop\Parent.xml");
            mySerializer.Serialize(myWriter, myObject);
            myWriter.Close();
        }
    }
    public struct Child
    {
        public string Name { get; set; }
        public string Code { get; set; }
    }

    public class Parent
    {
        public Child Child { get; set; }
    }
}


这是输出.

<?xml version="1.0" encoding="utf-8"?>
<Parent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Child />
</Parent>

>> 如果您要像下面那样反序列化xml,可以尝试以下代码.

IF you want to deserialize the xml like below, you could try the following code.

<Parent>
  <Child>PA</Child>
</Parent>

这是代码.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace deserialize_object
{
    class Program
    {
        static void Main(string[] args)
        {
            Parent myObject;
            // Construct an instance of the XmlSerializer with the type  
            // of object that is being deserialized.  
            XmlSerializer mySerializer =
            new XmlSerializer(typeof(Parent));
            // To read the file, create a FileStream.  
            FileStream myFileStream =
            new FileStream(@"C:\Users\v-wezan\Desktop\Parent.xml", FileMode.Open);
            // Call the Deserialize method and cast to the object type.  
            myObject = (Parent)mySerializer.Deserialize(myFileStream);
            Console.WriteLine(myObject.Child);
            Console.ReadKey();
        }
    }

    public class Parent
    {
        public string Child { get; set; }
    }
}

这是输出.

>> 反序列化xml文件和JSON文件之间的区别.

It is different between deserializing xml file and JSON file.

这是JSON反序列化的一种简单方法.

Here is a simple way for JSON to deserialize.

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;

namespace json_file
{
    class Program
    {
        static void Main(string[] args)
        {            
            string JSON = @"{
    'base': 'stations', 
    'visibility': 16093, 
    'dt': 1479110160,  
    'id': 5375480, 
    'name': 'Mountain View', 
    'cod': 200
}";
            RootObject obj = JsonConvert.DeserializeObject<RootObject>(JSON);
            Console.WriteLine(obj.id);
            Console.ReadKey();
        }

    }
    public class RootObject
    {
        public string @base { get; set; }
        public int visibility { get; set; } 
        public int dt { get; set; }
        public int id { get; set; }
        public string name { get; set; }
        public int cod { get; set; }
    }
}

我希望这会有所帮助.

最好的问候,

温迪


这篇关于c#结构可以序列化为“值类型"吗?还是它的特性之一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆