C# 学习笔记
CSV
优点:
解析简单,可以通过wps,excel来编辑,使用方便
缺点:
注意事项:
-
WPS另存为csv文件,这个时候文件的编码格式应该是ansi,C#默认读取文件是utf8格式,编码格式不对时,会乱码。
-
VS代码中的Encoding.ASCII并不是ANSI编码的意思;
使用GB2312 以代表ANSI编码
string fileName = "D:\\1234.txt";
StreamWriter sw = new StreamWriter(fileName,false, Encoding.GetEncoding("GB2312"));
sw.WriteLine("col1,col2,col3");
sw.WriteLine("繁體中文,2,3");
sw.WriteLine("简体中文,2,3");
sw.WriteLine("English,2,3");
sw.Close();
sw.Dispose();
DataTable
设置表头
DataTable SPClogDataTable=new DataTable();
SPClogDataTable.Columns[0].ColumnName="列头1";
相关文章
C# dataGridView 如何改变标题内容以及如何给表头内容换颜色
C# DataTable用法详解
xml
序列化
- 类可以直接序列化成xml文档。
- 带了
XmlAttributeAttribute
特性的字段和属性,会转换为特性 - 类转换成xml文档时,字段在前,属性在后,按代码先后顺序排列。
- 可以修改保存名字。
参考文章:
XML 序列化示例
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
namespace WindowsFormsApp3
{
//[TypeConverter(typeof(ExpandableObjectConverter))]
public class MachineManager
{
public enum eAxisList : int
{
轴X = 1000,
轴Y = 1500,
轴Z = 1300,
}
public enum eOutputList : int
{
启动灯 = 10,
停止灯 = 15,
闪光灯 = 18,
}
public enum eInputList : int
{
启动按钮 = 1600,
停止按钮 = 3500,
关闭按钮 = 37895,
}
public List<AxisParam> axes { get; set; } = new List<AxisParam>();
public List<OutputParam> Outputs { get; set; } = new List<OutputParam>();
public List<InputParam> Inputs { get; set; } = new List<InputParam>();
public void Init()
{
axes.Add(new AxisParam());
axes.Add(new AxisParam());
Outputs.Add(new OutputParam());
Inputs.Add(new InputParam());
object obj = this;
string path = @"test.xml";
XmlSerializer serializer = new XmlSerializer(obj.GetType());
TextWriter writer = new StreamWriter(path);
serializer.Serialize(writer, obj);
writer.Close();
//XmlAttribute temp = new XmlAttribute();
}
}
public class GlobalParam
{
public static int idcreat = 10000;
}
//[TypeConverter(typeof(ExpandableObjectConverter))]
public class AxisParam
{
[XmlAttribute("字段保存成特性")]
public int Id;
[XmlAttribute("属性保存成特性")]
public string Name { set; get; } = "未定义";
[XmlElement("卡号")]
public int CardId { set; get; } = 1;
public int AxisId { set; get; } = 1;
public string Line1 = "line1";
public string Name1="name1";
[XmlElement("字段保存成元素")]
public string ABC = "name1";
public AxisParam()
{
Id = GlobalParam.idcreat++;
}
}
//[TypeConverter(typeof(ExpandableObjectConverter))]
public class OutputParam
{
public int Id { set; get; }
public string Name { set; get; } = "未定义";
public int CardId { set; get; } = 1;
public int AxisId { set; get; } = 1;
public OutputParam()
{
Id = GlobalParam.idcreat++;
}
}
//[TypeConverter(typeof(ExpandableObjectConverter))]
public class InputParam
{
public int Id { set; get; }
public string Name { set; get; } = "未定义";
public int CardId { set; get; } = 1;
public int AxisId { set; get; } = 1;
public int grid = 1;
public InputParam()
{
Id = GlobalParam.idcreat++;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<MachineManager xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<axes>
<AxisParam 字段保存成特性="10000" 属性保存成特性="未定义">
<Line1>line1</Line1>
<Name1>name1</Name1>
<字段保存成元素>name1</字段保存成元素>
<卡号>1</卡号>
<AxisId>1</AxisId>
</AxisParam>
<AxisParam 字段保存成特性="10001" 属性保存成特性="未定义">
<Line1>line1</Line1>
<Name1>name1</Name1>
<字段保存成元素>name1</字段保存成元素>
<卡号>1</卡号>
<AxisId>1</AxisId>
</AxisParam>
</axes>
<Outputs>
<OutputParam>
<Id>10002</Id>
<Name>未定义</Name>
<CardId>1</CardId>
<AxisId>1</AxisId>
</OutputParam>
</Outputs>
<Inputs>
<InputParam>
<grid>1</grid>
<Id>10003</Id>
<Name>未定义</Name>
<CardId>1</CardId>
<AxisId>1</AxisId>
</InputParam>
</Inputs>
</MachineManager>
xml内置数据类型:
3.3.1 string
3.3.2 boolean
3.3.3 decimal
3.3.4 float
3.3.5 double
3.3.6 duration
3.3.7 dateTime
3.3.8 time
3.3.9 date
3.3.10 gYearMonth
3.3.11 gYear
3.3.12 gMonthDay
3.3.13 gDay
3.3.14 gMonth
3.3.15 hexBinary
3.3.16 base64Binary
3.3.17 anyURI
3.3.18 QName
3.3.19 NOTATION
参考:
W3C XML Schema Definition Language (XSD) 1.1 Part 2: Datatypes
编程
导入图片
C#WinForm开发:如何将图片添加到项目资源文件(Resources)中
编程前,先将相关文件导入到资源文件中。
枚举
字符串转枚举
public enum ApplyType
{
Passive,
Buff,
SingleTarget,
MultiTarget
}
ApplyType type = (ApplyType)System.Enum.Parse(typeof(ApplyType), str_applytype);