博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
XML(子节点序列化反序列对象)读写
阅读量:6984 次
发布时间:2019-06-27

本文共 2777 字,大约阅读时间需要 9 分钟。

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Web;
namespace ConsoleApplication19
{
[DataContract]
public class Student
{
[DataMember]
public int? stuNo{get;set;}
[DataMember]
public string Name{get;set;}

[DataMember]

public string Sex{get;set;}
[DataMember]
public Decimal Age{get;set;}
}
class Program
{
/// <summary>
/// 在xml中获得实体配置
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static object getXmlInfo(Student obj)
{
XmlSerializer serializer = new XmlSerializer(typeof(Student));
object s;
using (MemoryStream ms = new MemoryStream())
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(("/Config/DefaultFormValue.xml"));
//xmlDoc.Load(@"C:\Users\Hom\Documents\visual studio 2013\Projects\getXml\getXml\student.xml");
XmlNode tempNode = xmlDoc.SelectSingleNode("/Initialization/" + typeof(Student).Name);
if (tempNode != null)
{
XmlDocument tempDoc = new XmlDocument();
XmlDeclaration xmldecl;
xmldecl = tempDoc.CreateXmlDeclaration("1.0", "utf-8", null);
tempDoc.AppendChild(xmldecl);
XmlNode temRoot = tempDoc.CreateElement(typeof(Student).Name);
tempDoc.AppendChild(temRoot);
temRoot.InnerXml = tempNode.InnerXml;
//tempDoc.Save(@"C:\Users\Hom\Documents\visual studio 2013\Projects\getXml\getXml\student1.xml");
tempDoc.Save(ms);
ms.Seek(0, SeekOrigin.Begin);
s = serializer.Deserialize(ms);
}
else
{
s = null;
}
}
return s;
}
/// <summary>
/// 在xml中写入默认配置
/// </summary>
/// <param name="obj"></param>
public static void setXmlInfo(Student obj)
{
XmlSerializer serializer = new XmlSerializer(typeof(Student));
using (MemoryStream ms = new MemoryStream())
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(("DefaultFormValue.xml"));
//xmlDoc.Load(@"C:\Users\Hom\Documents\visual studio 2013\Projects\getXml\getXml\student.xml");
XmlNode tempNode = xmlDoc.SelectSingleNode("/Initialization/" + typeof(Student).Name);
if (tempNode == null)
{
serializer.Serialize(ms, obj);
XmlDocument tempDoc = new XmlDocument();
tempDoc.LoadXml(Encoding.UTF8.GetString(ms.ToArray()));
XmlElement element = xmlDoc.CreateElement(typeof(Student).Name);
element.InnerXml = tempDoc.SelectSingleNode(typeof(Student).Name).InnerXml;
XmlNode root = xmlDoc.SelectSingleNode("Initialization");
root.AppendChild(element);
xmlDoc.Save(("DefaultFormValue.xml"));
//xmlDoc.Save(@"C:\Users\Hom\Documents\visual studio 2013\Projects\getXml\getXml\student.xml");
}
}
}
static void Main(string[] args)
{
Student s = new Student() { stuNo=1, Name="1",Sex="1",Age=1 };
setXmlInfo(s);
Console.ReadKey();
}
}
}

转载于:https://www.cnblogs.com/kexb/p/4560845.html

你可能感兴趣的文章
Linux Study之--RedHat EL6配置VNC server
查看>>
负载均衡集群之lvs
查看>>
(三) Graphivz 基本图片类型
查看>>
ubuntu 升级过程中断电时遇到的问题总汇
查看>>
Netscaler基于policy的log action
查看>>
删除sql server 表中的重复数据!
查看>>
水仙花数
查看>>
初识set集合
查看>>
怎么寻回调整分区后盘符丢失的数据
查看>>
警惕!MySQL成数据勒索新目标
查看>>
linux系统学习第一天
查看>>
eclipse的安卓开发插件『ADT』在线安装不成功的解决方案
查看>>
第12章,网络管理(下)网络基础配置
查看>>
DTU是什么 DTU种类及应用领域分析
查看>>
基于Zynq-7000高速数据采集解决方案
查看>>
【VMware vSAN 6.6】5.2.运行状况:我们有软硬件项目解决方案
查看>>
细数iOS上的那些安全防护
查看>>
tar命令常用参数解释
查看>>
SourceTree跳过Atlassian账号,免登陆,跳过初始设置
查看>>
刷屏的海底捞超级APP究竟是怎样与阿里云合作的
查看>>