C # xml operations and C # serialization

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.