C# 對XML的操作 及 C#的序列化

來源:互聯網
上載者:User

對於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>

 

主要介紹2種方式對此XML 進行讀取修改操作,

第一種是通過XmlDocument來讀取,第二種是序列化的讀取。

 

一,XmlDocument方式:

 

1,讀取 xml,並將NekiProperty 的 屬性名稱及對應的值儲存到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);}

 

這樣就可以通過dbMap["databaseName"] 來擷取其對應的值。

2,修改並儲存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));

 

二,通過序列化進行操作:

首先需要建立3個類: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;        }     }

然後就可以通過代碼來讀取其中的各個值

private const string SYS_PROPERTY = "config\\SysProperty.xml";SysProperty sysProperty = new SysProperty().LoadProperty(Path.Combine(SYS_PROPERTY));string ip = sysProperty.getPropertyValue("databaseIP");

 

註:代碼中未作任何異常捕捉

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.