zoukankan      html  css  js  c++  java
  • 利用XmlSerializer类的特性读写WITSML

    随着XML的越来越流行,读写XML成为了一项基本的技能。在.Net平台下,XmlSerializer类可以将对象模型直接序列化为XML文件,并支持反向序列化。这样的特性无疑给程序员带来了极大的便利。

    在石油行业,WITSML(井场信息传输标准标记语言)日益取代WITS,为新一代传输标准奠定了语言基础。如何读写WITSML迫在眉睫。

    这里将结合这样的技术和这样的需求,给出一个简单的示例,供相关人士参考批评。

    1. 需要引入System.IO和System.Xml.Serialization两个命名空间。
    2. 建立相应的类。
    3. 为对象赋值。
    4. 创建序列化对象
    5. 生成并写入XML文件
    6. 读取并获取XML文件中的对象
    7. 输出

    代码示例如下:

    /************************************************************************/
    /* 利用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; }
            }
        }
    }

    运行结果:

    image

    image

    作者:pezy 出处:http://www.cnblogs.com/pezy 欢迎转载,也请保留这段声明。谢谢!
  • 相关阅读:
    (OK) [android-x86-6.0-rc1] grub
    /proc/sys/net/ipv4/conf/*/promote_secondaries
    (OK) Ipsysctl tutorial 1.0.4
    Android Netd ndc (Native Daemon Connector)
    Android Wi-Fi — IP forward — ndc — netd
    Android 网络问题
    有哪些 Android 大牛的 blog 值得推荐?
    Android 4.4 Kitkat 使能有线网络 Ethernet
    IP forwarding failure in android 4.0
    (OK) [android-x86-6.0-rc1] /system/etc/init.sh
  • 原文地址:https://www.cnblogs.com/pezy/p/2416835.html
Copyright © 2011-2022 走看看