写配置文件
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; } }