• 读写配置文件


    写配置文件

    public void WriteConfigurationFile(string path)
            {
                using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(path, true))
                {
                    if (_vdtSource == null || _vdtSource.Rows.Count == 0)
                    {
                        return;
                    }
     
                    string sourceNode = _vdtSource.Rows[0]["TblName"].ToString();
                    string currentNode = string.Empty;
                    streamWriter.WriteLine("<DUMP>");
                    streamWriter.WriteLine(_vsNodeFormat,
                            _vdtSource.Rows[0]["TblName"].ToString(),
                            _vdtSource.Rows[0]["Valid"].ToString(),
                            "",
                            ""
                        );
     
                    foreach (System.Data.DataRow row in _vdtSource.Rows)
                    {
                        currentNode = row["TblName"].ToString();
                        if (currentNode == sourceNode)
                        {
                            streamWriter.WriteLine(_vsFieldFormat, row["ColName"].ToString(), row["ColIndex"]);
                        }
                        else
                        {
                            streamWriter.WriteLine("    </Node>");
                            sourceNode = row["TblName"].ToString();
                            streamWriter.WriteLine(_vsNodeFormat,
                                row["TblName"].ToString(),
                                row["Valid"].ToString(),
                                "",
                                ""
                            );
                        }
                    }
                    streamWriter.WriteLine("    </Node>");
                    streamWriter.WriteLine("</DUMP>");
                }
            }

    读配置文件

    public class NodeInfoReader
        {
            public static NodeMode GetNode(string nodeName, string path)
            {
                System.Xml.XmlReaderSettings xmlReaderSettings = new System.Xml.XmlReaderSettings();
                xmlReaderSettings.ProhibitDtd = false;
                System.IO.FileStream fileStream = new System.IO.FileStream(
                        path,
                        System.IO.FileMode.Open,
                        System.IO.FileAccess.Read,
                        System.IO.FileShare.ReadWrite
                    );
                fileStream.Seek(0, System.IO.SeekOrigin.Begin);
                using (System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(fileStream, xmlReaderSettings))
                {
                    string currentNodeName = string.Empty;
                    NodeMode node = null;
                    List<string> lstField = null;
                    while (xmlReader.Read())
                    {
                        switch (xmlReader.NodeType)
                        {
                            case System.Xml.XmlNodeType.Element:
                                if (xmlReader.Name == "Node")
                                {
                                    currentNodeName = xmlReader.GetAttribute("Name");
                                    if (currentNodeName == nodeName)
                                    {
                                        node = new NodeMode();
                                        lstField = new List<string>();
     
                                        node.Name = currentNodeName;
                                        node.Valid = int.Parse(xmlReader.GetAttribute("Valid"));
                                        node.ParseMethod = xmlReader.GetAttribute("ParseMethod");
                                        node.NullHandler = xmlReader.GetAttribute("NullHandler");
                                    }
                                }
                                else
                                {
                                    if (lstField != null)
                                    {
                                        lstField.Add(xmlReader.GetAttribute("Field"));
                                    }
                                }
                                break;
                            case System.Xml.XmlNodeType.EndElement:
                                if (xmlReader.Name == "Node" && node != null)
                                {
                                    node.Field = lstField;
                                    return node;
                                }
                                break;
                        }
                    }
                }
                return null;
            }
        }
     
        public class NodeMode
        {
            public string Name { get; set; }
            public int Valid { get; set; }
            public string ParseMethod { get; set; }
            public string NullHandler { get; set; }
            public List<string> Field { get; set; }
        }
  • 相关阅读:
    wamp的安装
    原型模式(clone模式)
    观察者模式observer(用于不同类的中 板块的显示或其它操作)
    数据对象映射模式(通过工厂模式和注册树模式)v2
    数据对象映射模式(及对象的相关属性和数据库表中的字段一一对应)
    策略模式根据不同的性别给出不同的推荐
    适配器模式(数据库方面)支持不同的数据库连接
    php的三种设计模式
    UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only解决办法
    SQL语句如何在同一个表内复制一条记录
  • 原文地址:https://www.cnblogs.com/hongjiumu/p/3145081.html
Copyright © 2020-2023  润新知