From 7b903bb86375e9df24a725288d1c876b8d2b0e61 Mon Sep 17 00:00:00 2001
From: chr <chr@mailcom>
Date: 星期一, 03 三月 2025 19:47:49 +0800
Subject: [PATCH] 整合TestResult;
---
HMI.Core/Util/XmlHelper.cs | 32 ++++++++++++++++++++++++++++----
1 files changed, 28 insertions(+), 4 deletions(-)
diff --git a/HMI.Core/Util/XmlHelper.cs b/HMI.Core/Util/XmlHelper.cs
index d0dba0d..6006684 100644
--- a/HMI.Core/Util/XmlHelper.cs
+++ b/HMI.Core/Util/XmlHelper.cs
@@ -1,6 +1,10 @@
锘縰sing System;
+using System.CodeDom;
+using System.Collections.Generic;
using System.IO;
using System.Reflection;
+using System.Text;
+using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Serialization;
using static System.Net.WebRequestMethods;
@@ -9,19 +13,27 @@
{
public static class XmlHelper
{
+
+ static readonly XmlSerializerNamespaces DEFAULT_NS = new XmlSerializerNamespaces();
+
+ static XmlHelper()
+ {
+ DEFAULT_NS.Add("", "");
+ }
+
/// <summary>
/// serialize object to xml file.
/// </summary>
/// <param name="path">the path to save the xml file</param>
/// <param name="obj">the object you want to serialize</param>
public static void SerializeToXml(string path, object obj)
- {
+ {
XmlSerializer serializer = new XmlSerializer(obj.GetType());
string content = string.Empty;
//serialize
using (StringWriter writer = new StringWriter())
{
- serializer.Serialize(writer, obj);
+ serializer.Serialize(writer, obj, DEFAULT_NS);
content = writer.ToString();
}
//save to file
@@ -45,13 +57,15 @@
return (T)serializer.Deserialize(reader);
}
}
- public static bool ReadFromXml<T>(T obj, string path) {
+ public static bool ReadFromXml<T>(T obj, string path)
+ {
XmlDocument xd = new XmlDocument();
xd.Load(path);
Type type = typeof(T);
XmlNode root = xd.SelectSingleNode(type.Name);
- if (root == null) {
+ if (root == null)
+ {
return false;
}
@@ -70,5 +84,15 @@
return true;
}
+ public static T DeepClone<T>(T data)
+ {
+ using (MemoryStream stream = new MemoryStream())
+ {
+ XmlSerializer serializer = new XmlSerializer(typeof(T));
+ serializer.Serialize(stream, data);
+ stream.Seek(0, SeekOrigin.Begin);
+ return (T)serializer.Deserialize(stream);
+ }
+ }
}
}
--
Gitblit v1.9.1