在WinForm中處理XML和JSON數(shù)據(jù)可以通過使用.NET Framework提供的相關(guān)類和方法來實(shí)現(xiàn)。以下是處理XML和JSON數(shù)據(jù)的一些常用方法:
處理XML數(shù)據(jù):
XmlDocument doc = new XmlDocument();
doc.Load("data.xml");
XmlNode node = doc.SelectSingleNode("/root/node");
string value = node.InnerText;
XmlSerializer serializer = new XmlSerializer(typeof(MyObject));
MyObject obj = new MyObject();
XmlWriter writer = XmlWriter.Create("data.xml");
serializer.Serialize(writer, obj);
處理JSON數(shù)據(jù):
string json = File.ReadAllText("data.json");
JObject obj = JObject.Parse(json);
string value = (string)obj["key"];
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(MyObject));
MyObject obj = new MyObject();
FileStream file = new FileStream("data.json", FileMode.Create);
serializer.WriteObject(file, obj);
通過以上方法,可以在WinForm應(yīng)用程序中輕松地處理XML和JSON數(shù)據(jù),實(shí)現(xiàn)數(shù)據(jù)的讀取、解析、序列化和反序列化等操作。