随着XML的越来越流行,读写XML成为了一项基本的技能。在.Net平台下,XmlSerializer类可以将对象模型直接序列化为XML文件,并支持反向序列化。这样的特性无疑给程序员带来了极大的便利。
在石油行业,WITSML(井场信息传输标准标记语言)日益取代WITS,为新一代传输标准奠定了语言基础。如何读写WITSML迫在眉睫。
这里将结合这样的技术和这样的需求,给出一个简单的示例,供相关人士参考批评。
- 需要引入System.IO和System.Xml.Serialization两个命名空间。
- 建立相应的类。
- 为对象赋值。
- 创建序列化对象
- 生成并写入XML文件
- 读取并获取XML文件中的对象
- 输出
代码示例如下:
/************************************************************************/ /* 利用Xml.Serialization来读写XML文件。这里以WITSML的读写为例。 */ /************************************************************************/ #region Using directives using System; using System.Collections.Generic; using System.IO; using System.Xml.Serialization; #endregion namespace XmlSerializerTest { class Program { static void Main(string[] args) { // Create and initialize a wellbores instance wellbores instance = new wellbores(); instance.documentInfo = new documentInfoType(); instance.documentInfo.DocumentName = "wellbore"; instance.documentInfo.FileCreationInformation = new FileCreationInformationType(); instance.documentInfo.FileCreationInformation.FileCreationDate = "2012-03-25 08:15:00"; instance.documentInfo.FileCreationInformation.SoftwareName = "Simulate"; instance.documentInfo.FileCreationInformation.FileCreator = "pezy"; instance.wellbore = new wellboreType(); instance.wellbore.uidWell = "W-12"; instance.wellbore.uid = "B-001"; instance.wellbore.nameWell = "6507/7-A-42"; instance.wellbore.number = 1234; // Create the serializer XmlSerializer serializer = new XmlSerializer(typeof(wellbores)); // Serialize the object to an XML file using (StreamWriter streamWriter = File.CreateText( "wellbore.xml")) { serializer.Serialize(streamWriter, instance); } // Deserialize the object wellbores deserializedInstance; using (StreamReader streamReader = File.OpenText( "wellbore.xml")) { deserializedInstance = serializer.Deserialize(streamReader) as wellbores; } // Dump the object Console.WriteLine("DocumentName: {0}",deserializedInstance.documentInfo.DocumentName); Console.WriteLine("FileCreationDate: {0}", deserializedInstance.documentInfo.FileCreationInformation.FileCreationDate); Console.WriteLine("SoftwareName: {0}", deserializedInstance.documentInfo.FileCreationInformation.SoftwareName); Console.WriteLine("FileCreator: {0}", deserializedInstance.documentInfo.FileCreationInformation.FileCreator); Console.WriteLine("uidWell: {0}", deserializedInstance.wellbore.uidWell); Console.WriteLine("uid: {0}", deserializedInstance.wellbore.uid); Console.WriteLine("nameWell: {0}", deserializedInstance.wellbore.nameWell); Console.WriteLine("number: {0}", deserializedInstance.wellbore.number); } } // <wellbores> [Serializable()] public class wellbores { private documentInfoType documentInfoValue; public documentInfoType documentInfo { get { return documentInfoValue; } set { documentInfoValue = value; } } private wellboreType wellboreValue; public wellboreType wellbore { get { return wellboreValue; } set { wellboreValue = value; } } } // <documentInfo> [Serializable()] public class documentInfoType { private string documentName; public string DocumentName { get { return documentName; } set { documentName = value; } } private FileCreationInformationType fileCreationInformation; public FileCreationInformationType FileCreationInformation { get { return fileCreationInformation; } set { fileCreationInformation = value; } } } // <FileCreationInformation> [Serializable()] public class FileCreationInformationType { private string fileCreationDate; public string FileCreationDate { get { return fileCreationDate; } set { fileCreationDate = value; } } private string softwareName; public string SoftwareName { get { return softwareName; } set { softwareName = value; } } private string fileCreator; public string FileCreator { get { return fileCreator; } set { fileCreator = value; } } } // <wellbore> [Serializable()] public class wellboreType { private string uidWellValue; public string uidWell { get { return uidWellValue; } set { uidWellValue = value; } } private string uidValue; public string uid { get { return uidValue; } set { uidValue = value; } } private string nameWellValue; public string nameWell { get { return nameWellValue; } set { nameWellValue = value; } } private int numberValue; public int number { get { return numberValue; } set { numberValue = value; } } } }
运行结果: