zoukankan      html  css  js  c++  java
  • 读写配置文件

    写配置文件

    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; }
        }
  • 相关阅读:
    k8s命令
    git绿色、红色图标不显示的问题
    Git下载
    文档(PDF Word Excel PPT)转HTML前端预览方案
    腾讯云生成临时访问链接
    cron表达式的双重人格:星期和数字到底如何对应?
    Windows下nginx报错解决:CreateFile() "xxx/logs/nginx.pid" failed
    Windows命令行在任意位置启动和退出nginx
    解决博客园TinyMCE模式下内置插入代码块功能不支持Go语言的问题(两个并不完美的解决方案)
    linux系统调用system()函数详解
  • 原文地址:https://www.cnblogs.com/hongjiumu/p/3145081.html
Copyright © 2011-2022 走看看