visual studio使用技巧:快速生成Json、XML对应类

visual studio快速生成Json、XML对应类

在项目中经常用到json或者xml作为配置文件,进行序列化和反序列化就需要有对应的类,重新写一遍类就比较麻烦,这里就讲一下通过visual studio快速生成json或者xml对应类型的方法。

自动生成Json类

复制一个json文件,自动在visual studio中生成

复制Json文件

json 复制代码
{
  "DeviceLinks": [
    {
      "UID": "device01",
      "Ip": "127.0.0.1",
      "Port": 502,
      "SlaveId": 1,
      "AcqTimeSpan": 1
    },
    {
      "UID": "device02",
      "Ip": "127.0.0.1",
      "Port": 503,
      "SlaveId": 1,
      "AcqTimeSpan": 1
    }
  ],
  "MqttConfig": {
    "Ip": "127.0.0.1",
    "Port": 1883,
    "Username": "admin",
    "Password": "12345"
  },
  "ServiceConfig": {
    "PushTimeSpan": 5,
    "IsPushScheduled": true,
    "IsPushChanged": true
  }
}

切换到一个cs的类文件

必须在c#的cs文件中,才能看到编辑中对应的菜单

选择性粘贴

在菜单栏,"编辑"→"选择性粘贴"→将JSON粘贴为类

代码自动生成,会生成一个Rootobject为json的主类,子类也会一起生成,这里根据json字段定义,自动生成对应的子类

csharp 复制代码
public class Rootobject
{
    public Devicelink[] DeviceLinks { get; set; }
    public Mqttconfig MqttConfig { get; set; }
    public Serviceconfig ServiceConfig { get; set; }
}

public class Mqttconfig
{
    public string Ip { get; set; }
    public int Port { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
}

public class Serviceconfig
{
    public int PushTimeSpan { get; set; }
    public bool IsPushScheduled { get; set; }
    public bool IsPushChanged { get; set; }
}

public class Devicelink
{
    public string UID { get; set; }
    public string Ip { get; set; }
    public int Port { get; set; }
    public int SlaveId { get; set; }
    public int AcqTimeSpan { get; set; }
}

注意:转换大小写,会跟随json文件,需要自己调整一下

自动生成XML类

步骤一样,选择性粘贴中选择"将XML粘贴为类"

示例XML:

xml 复制代码
<Configuration>
  <DeviceLinks>
    <DeviceLink>
      <UID>device01</UID>
      <Ip>127.0.0.1</Ip>
      <Port>502</Port>
      <SlaveId>1</SlaveId>
      <AcqTimeSpan>1</AcqTimeSpan>
    </DeviceLink>
    <DeviceLink>
      <UID>device02</UID>
      <Ip>127.0.0.1</Ip>
      <Port>503</Port>
      <SlaveId>1</SlaveId>
      <AcqTimeSpan>1</AcqTimeSpan>
    </DeviceLink>
  </DeviceLinks>
  
  <MqttConfig>
    <Ip>127.0.0.1</Ip>
    <Port>1883</Port>
    <Username>admin</Username>
    <Password>12345</Password>
  </MqttConfig>
  
  <ServiceConfig>
    <PushTimeSpan>5</PushTimeSpan>
    <IsPushScheduled>true</IsPushScheduled>
    <IsPushChanged>true</IsPushChanged>
  </ServiceConfig>
</Configuration>

生成结果

csharp 复制代码
// 注意: 生成的代码可能至少需要 .NET Framework 4.5 或 .NET Core/Standard 2.0。
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Configuration
{

    private ConfigurationDeviceLink[] deviceLinksField;

    private ConfigurationMqttConfig mqttConfigField;

    private ConfigurationServiceConfig serviceConfigField;

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("DeviceLink", IsNullable = false)]
    public ConfigurationDeviceLink[] DeviceLinks
    {
        get
        {
            return this.deviceLinksField;
        }
        set
        {
            this.deviceLinksField = value;
        }
    }

    /// <remarks/>
    public ConfigurationMqttConfig MqttConfig
    {
        get
        {
            return this.mqttConfigField;
        }
        set
        {
            this.mqttConfigField = value;
        }
    }

    /// <remarks/>
    public ConfigurationServiceConfig ServiceConfig
    {
        get
        {
            return this.serviceConfigField;
        }
        set
        {
            this.serviceConfigField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ConfigurationDeviceLink
{

    private string uIDField;

    private string ipField;

    private ushort portField;

    private byte slaveIdField;

    private byte acqTimeSpanField;

    /// <remarks/>
    public string UID
    {
        get
        {
            return this.uIDField;
        }
        set
        {
            this.uIDField = value;
        }
    }

    /// <remarks/>
    public string Ip
    {
        get
        {
            return this.ipField;
        }
        set
        {
            this.ipField = value;
        }
    }

    /// <remarks/>
    public ushort Port
    {
        get
        {
            return this.portField;
        }
        set
        {
            this.portField = value;
        }
    }

    /// <remarks/>
    public byte SlaveId
    {
        get
        {
            return this.slaveIdField;
        }
        set
        {
            this.slaveIdField = value;
        }
    }

    /// <remarks/>
    public byte AcqTimeSpan
    {
        get
        {
            return this.acqTimeSpanField;
        }
        set
        {
            this.acqTimeSpanField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ConfigurationMqttConfig
{

    private string ipField;

    private ushort portField;

    private string usernameField;

    private ushort passwordField;

    /// <remarks/>
    public string Ip
    {
        get
        {
            return this.ipField;
        }
        set
        {
            this.ipField = value;
        }
    }

    /// <remarks/>
    public ushort Port
    {
        get
        {
            return this.portField;
        }
        set
        {
            this.portField = value;
        }
    }

    /// <remarks/>
    public string Username
    {
        get
        {
            return this.usernameField;
        }
        set
        {
            this.usernameField = value;
        }
    }

    /// <remarks/>
    public ushort Password
    {
        get
        {
            return this.passwordField;
        }
        set
        {
            this.passwordField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ConfigurationServiceConfig
{

    private byte pushTimeSpanField;

    private bool isPushScheduledField;

    private bool isPushChangedField;

    /// <remarks/>
    public byte PushTimeSpan
    {
        get
        {
            return this.pushTimeSpanField;
        }
        set
        {
            this.pushTimeSpanField = value;
        }
    }

    /// <remarks/>
    public bool IsPushScheduled
    {
        get
        {
            return this.isPushScheduledField;
        }
        set
        {
            this.isPushScheduledField = value;
        }
    }

    /// <remarks/>
    public bool IsPushChanged
    {
        get
        {
            return this.isPushChangedField;
        }
        set
        {
            this.isPushChangedField = value;
        }
    }
}
相关推荐
读书札记202212 小时前
visual studio 调试技巧总结
ide·visual studio
Shadow(⊙o⊙)16 小时前
智能指针、循环引用、锁、删除器
开发语言·c++·后端·visual studio
hacker70716 小时前
Visual Studio安装教程(C#开发版)
ide·c#·visual studio
AC赳赳老秦18 小时前
团队知识库搭建:用 OpenClaw 自动整理会议纪要、技术方案、故障复盘,同步到 Confluence / 语雀
开发语言·前端·python·github·visual studio·deepseek·openclaw
ID_180079054731 天前
Python 实现亚马逊商品详情 API 数据准确性校验(极简可用 + JSON 参考)
java·python·json
代码AI弗森1 天前
配置文件战争:TOML/YAML/JSON 为何成为不同框架的“专属标配”?
json
不会编程的懒洋洋2 天前
WPF XAML+布局+控件
xml·开发语言·c#·视觉检测·wpf·机器视觉·视图
时光之源2 天前
Visual Studio | Marketplace创建发布者(Create Publisher)时无法创建的问题解决方案
ide·vscode·visual studio·plugin·cursor
lifewange2 天前
CNode API v1 完整接口文档(JSON 规范整理)
java·前端·json
测试修炼手册2 天前
[测试技术] 深入理解 JSON Web Token (JWT)
前端·json