For XML: sysproperty. xml
<?xml version="1.0" encoding="utf-8"?><SysProperty><SysContext><NekiProperty propertyName="databaseName" propertyValue="PMSDB" /><NekiProperty propertyName="databaseIP" propertyValue="192.168.1.186" /><NekiProperty propertyName="databasePort" propertyValue="3306" /><NekiProperty propertyName="databaseUser" propertyValue="root" /><NekiProperty propertyName="databasePassword" propertyValue="root" /></SysContext></SysProperty>
This article mainly introduces two ways to read and modify this XML,
The first is reading through xmldocument, and the second is serialized reading.
I. xmldocument method:
1. Read XML and save the attribute name and value of nekiproperty to dictionary.
private const string SYS_PROPERTY = "config\\SysProperty.xml";string totalPath = Path.Combine(Application.StartupPath, SYS_PROPERTY);XmlDocument doc = new XmlDocument();doc.Load(totalPath);XmlNode root = doc.SelectSingleNode("/SysProperty/SysContext");XmlNodeList childrenList = root.ChildNodes;dbMap = new Dictionary<string, string>();foreach (XmlNode temp in childrenList){dbMap.Add(temp.Attributes["propertyName"].Value, temp.Attributes["propertyValue"].Value);}
In this way, you can use dbmap ["databasename"] to obtain the corresponding value.
2. Modify and save the values in XML.
foreach (XmlNode temp in childrenList){if (temp.Attributes["propertyValue"].Value == "mysql"){XmlElement xe = temp as XmlElement;xe.SetAttribute("propertyValue", "clothes");break;}}doc.Save(Path.Combine(Application.StartupPath, SYS_PROPERTY));
2. perform operations through serialization:
You must first create three classes: sysproperty. CS; syscontext. CS; nekiproperty. CS
public class SysProperty { private SysContext sysContext; public SysContext SysContext { get { return sysContext; } set { sysContext = value; } } public SysProperty LoadProperty(string file) { XmlSerializer xs = new XmlSerializer(typeof(SysProperty)); StreamReader sr = new StreamReader(file); SysProperty sysProperty = xs.Deserialize(sr) as SysProperty; sr.Close(); return sysProperty; } public void saveConfig(string file) { XmlSerializer xs = new XmlSerializer(typeof(SysProperty)); StreamWriter sw = new StreamWriter(file); xs.Serialize(sw, this); sw.Close(); } public string getPropertyValue(string propertyName) { foreach (NekiProperty property in sysContext) { if (property.PropertyName == propertyName) { return property.PropertyValue; } } return string.Empty; } }
public class SysContext :List<NekiProperty> { }
public class NekiProperty { private string propertyName = ""; private string propertyValue = ""; [XmlAttribute("propertyName")] public string PropertyName { get { return propertyName; } set { propertyName = value; } } [XmlAttribute("propertyValue")] public string PropertyValue { get { return propertyValue; } set { propertyValue = value; } } public NekiProperty() { } public NekiProperty(string propertyName, string propertyValue) { this.propertyName = propertyName; this.propertyValue = propertyValue; } }
Then you can read the values in the code.
private const string SYS_PROPERTY = "config\\SysProperty.xml";SysProperty sysProperty = new SysProperty().LoadProperty(Path.Combine(SYS_PROPERTY));string ip = sysProperty.getPropertyValue("databaseIP");
Note: No exceptions are caught in the code.