.xml文件读取与写入
[实现效果]
[实现编程]
/// <summary>
/// 创建xml配置
/// </summary>
/// <param name="path">path ,如:x:\\xxxx\\xxx.xml</param>
private void createXmlText(string path)
{
if (string.IsNullOrEmpty(path))
return;
if (File.Exists(path))
return;
XDocument document = new XDocument();
XElement root = new XElement("参数");
root.Add(new XComment("输入图像:采集算子编号"));
root.SetElementValue("输入图像", "001");
root.SetElementValue("X偏移", "1");
root.SetElementValue("Y偏移", "1");
root.Add(new XComment("结果图像:图像变量的名称"));
root.SetElementValue("结果图像", "定位图");
root.Save(path);
}
/// <summary>
/// 读取xml配置
/// </summary>
/// <param name="path">path ,如:x:\\xxxx\\xxx.xml</param>
/// <returns></returns>
private Config readXml(string path)
{
if (string.IsNullOrEmpty(path))
return null;
if (!File.Exists(path))
return null;
XDocument document = XDocument.Load(path);
XElement root = document.Root;
//读取配置文件
XElement inputImageNo = root.Element("输入图像");
XElement offsetX = root.Element("X偏移");
XElement offsetY = root.Element("Y偏移");
XElement resultImageVariableName = root.Element("结果图像");
//转换
Config config = new Config();
config.InputImageNo = inputImageNo.Value;
config.OffsetX = XmlConvert.ToInt32(offsetX.Value == "" ? "0" : offsetX.Value);
config.OffsetY = XmlConvert.ToInt32(offsetY.Value == "" ? "0" : offsetY.Value);
config.ResultImageVariableName = resultImageVariableName.Value;
return config;
}
internal class Config
{
public string InputImageNo { get; set; }
public int OffsetX { get; set; }
public int OffsetY { get; set; }
public string ResultImageVariableName { get; set; }
}
[实现编程:序列化的方式]
/// <summary>
/// 保存xml文件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="dir">文件目录,如 D:\\Config</param>
/// <param name="name">文件名称,如 config.xml</param>
public bool Save<T>(T list, string dir, string name)
{
try
{
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
var filePath = System.IO.Path.Combine(dir, name);
//如果文件存在需要删除,否则会续写,造成格式错误
if (File.Exists(filePath))
File.Delete(filePath);
using (System.IO.StringWriter stringWriter = new StringWriter(new StringBuilder()))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
xmlSerializer.Serialize(stringWriter, list);
FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
sw.Write(stringWriter.ToString());
sw.Close();
fs.Close();
}
}
catch (System.Exception ex)
{
return false;
}
return true;
}
/// <summary>
/// 读取xml文件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="path">文件路径,如 D:\\Config\\CameraSetting.xml</param>
/// <returns></returns>
public T Read<T>(string path)
{
if (!File.Exists(path))
return default;
try
{
using (TextReader reader = new StreamReader(path))
{
var serializer = new XmlSerializer(typeof(T));
var items = (T)serializer.Deserialize(reader);
return items;
}
}
catch (System.Exception ex)
{
}
return default;
}
[调用方式 示例]
string path = "D:\\Config\\CameraSetting.xml";
// 读
xxxx = Read<List<Camera>>(path);
// 存
Save(path);
标签:xml,文件,return,读取,C#,new,path,root,string
From: https://www.cnblogs.com/moon-stars/p/18431401