首页 > 其他 > 详细

Csharp读写XML

时间:2014-02-28 17:34:50      阅读:1009      评论:0      收藏:0      [点我收藏+]

保存
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Initialize.xml");
XmlNodeList nodeList = xmlDoc.SelectSingleNode("ComValue").ChildNodes;//获取 节点的所有子节点
foreach (XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
if (xe.GetAttribute("name") == "PortName")
{
xe.InnerText = com.PortName.ToString();
}
if (xe.GetAttribute("name") == "BaudRate")
{
xe.InnerText = com.BaudRate.ToString();
}
if (xe.GetAttribute("name") == "Parity")
{
xe.InnerText = com.Parity.ToString();
}
if (xe.GetAttribute("name") == "StopBits")
{
xe.InnerText = com.StopBits.ToString();
}
}
xmlDoc.Save("Initialize.xml");//保存。

读取

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Initialize.xml");
XmlNodeList nodeList = xmlDoc.SelectSingleNode("ComValue").ChildNodes;//获取 节点的所有子节点
foreach (XmlNode xn in nodeList)//遍历所有子节点
{

XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
if (xe.GetAttribute("name") == "PortName")
{
com.PortName = xe.InnerText;
}
if (xe.GetAttribute("name") == "BaudRate")
{
com.BaudRate = int.Parse(xe.InnerText);
}
if (xe.GetAttribute("name") == "Parity")
{
com.Parity = Parity.None;
}
if (xe.GetAttribute("name") == "StopBits")
{
com.StopBits = StopBits.One;
}
}

 

本文出自 “共同畅游net” 博客,请务必保留此出处http://youarenotalone.blog.51cto.com/3210177/1364474

Csharp读写XML,布布扣,bubuko.com

Csharp读写XML

原文:http://youarenotalone.blog.51cto.com/3210177/1364474

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!