上一篇https://www.cnblogs.com/huvjie/p/15351027.html可以在原有的 xml 文档里添加一个序列化对象的元素节点。接的的 Name 名字默认的是对象类型名,如何更改这个名字呢?另外将一个 XElement 节点返序列化成一个对象。
- 直接对 XElement 的 Name 属性赋值就能改名。
- 将一个 XElement 节点返序列化,和从文档加载区别不大,注意将更改的 Name 属性名改回去,不然加载出错。
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Sex { get; set; }
[XmlElement("Dog")]
public List<Dog> Dog { get; set; }
public string Good { get; set; } = "100";
private string valuePrivate { get; set; } = "0";
}
public class Dog
{
public string Name { get; set; }
public int Age { get; set; }
}
XmlSerializerHelper
增加了一个,将 XElement节点返序列化成一个对象。这个XmlSerializerHelper
序列化返序列化类应该是比较完整了。
class XmlSerializerHelper
{
public void XmlSerial(object obj, string xmlFile)
{
// 序列化
using (MemoryStream ms = new MemoryStream())
{
XmlWriterSettings setting = new XmlWriterSettings() {
Encoding = new UTF8Encoding(false),
Indent = true,
};
using (XmlWriter writer = XmlWriter.Create(ms, setting))
{
XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());
// 去掉命名空间
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", ""); // 命名空间赋值为空
xmlSerializer.Serialize(writer, obj, ns);
// 用StreamWriter
string xmlContent = Encoding.UTF8.GetString(ms.ToArray()); // 转成字符转
StreamWriter streamWriter = new StreamWriter(xmlFile);
streamWriter.Write(xmlContent);
streamWriter.Close();
}
}
}
public XElement XmlSerial(object obj)
{
// 序列化
using (MemoryStream ms = new MemoryStream())
{
XmlWriterSettings setting = new XmlWriterSettings() {
Encoding = new UTF8Encoding(false),
Indent = true,
};
using (XmlWriter writer = XmlWriter.Create(ms, setting))
{
XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());
// 去掉命名空间
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", ""); // 命名空间赋值为空
xmlSerializer.Serialize(writer, obj, ns);
ms.Position = 0; // 把流的位置弄到最前面,不然找不到根元素
XDocument objElem = XDocument.Load(ms);
XElement rootElem = objElem.Root;
// 可以直接加载
// XElement rootElem = XElement.Load(ms);
return rootElem;
}
}
}
// 返序列化
public T XmlDeserial<T>(string xmlFile)
{
// 用 MemoryStream
string xmlString = File.ReadAllText(xmlFile); // xml 内容提取出来
using (MemoryStream MS = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)))
{
using (XmlReader xr = XmlReader.Create(MS))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
return (T)xmlSerializer.Deserialize(xr); // 不要误写为 MS 流,有这个重载不会报错。但运行有错。
}
}
}
// 返序列化
public T XmlDeserial<T>(XElement xElem)
{
xElem.Name = typeof(T).Name; // 如果名称不对,出错。防止改了节点名的情况
// 用 MemoryStream
string xmlString = xElem.ToString(); // xml 内容提取出来
using (MemoryStream MS = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)))
{
using (XmlReader xr = XmlReader.Create(MS))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
return (T)xmlSerializer.Deserialize(xr); // 不要误写为 MS 流,有这个重载不会报错。但运行有错。
}
}
}
}
class Program
{
static void Main(string[] args)
{
List<Dog> dogs = new List<Dog> {
new Dog() { Name = "小黑", Age = 9 },
new Dog() { Name = "小黄", Age = 10 }
};
Person person = new Person() {
Name = "孙悟空",
Age = 500,
Sex = "男",
Dog = dogs
};
XmlSerializerHelper xmlSerializerHelper = new XmlSerializerHelper();
xmlSerializerHelper.XmlSerial(person, "Person.xml");
// 返序列化一下
Person dog_dd = xmlSerializerHelper.XmlDeserial<Person>("Person.xml");
Console.WriteLine(dog_dd.Name);
Console.ReadKey();
// 增加一个 Dog 类
XElement rootElem = XElement.Load("Person.xml");
rootElem.Add(xmlSerializerHelper.XmlSerial(new Dog() { Name = "小辉", Age = 15 }));
rootElem.Save("Person.xml");
/***********下面改下Name属性测试**********/
XElement rootElement = XElement.Load("Person.xml");
Dog newDog = new Dog() { Name = "狗蛋", Age = 100 };
XElement newDogElem = xmlSerializerHelper.XmlSerial(newDog);
newDogElem.Name = "GouDan"; // 直接改名字
rootElement.Add(newDogElem);
XElement gouDan = rootElement.Element("GouDan"); // 根目录加载出来
rootElement.Save("newPerson.xml");
Dog dog = xmlSerializerHelper.XmlDeserial<Dog>(gouDan);
Console.WriteLine("Name = {0}\nAge = {1}", dog.Name, dog.Age);
Console.ReadKey();
}
}
输出:
孙悟空
Name = 狗蛋
Age = 100
生成的 XMl 文档:
<?xml version="1.0" encoding="utf-8"?>
<Person>
<Name>孙悟空</Name>
<Age>500</Age>
<Sex>男</Sex>
<Dog>
<Name>小黑</Name>
<Age>9</Age>
</Dog>
<Dog>
<Name>小黄</Name>
<Age>10</Age>
</Dog>
<Good>100</Good>
<Dog>
<Name>小辉</Name>
<Age>15</Age>
</Dog>
<GouDan>
<Name>狗蛋</Name>
<Age>100</Age>
</GouDan>
</Person>
可见,上面最后添加的Dog
节点改成了GouDan
。