新聞中心
保存
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Initialize.xml");
XmlNodeList nodeList = xmlDoc.SelectSingleNode("ComValue").ChildNodes;//獲取 節(jié)點的所有子節(jié)點
foreach (XmlNode xn in nodeList)//遍歷所有子節(jié)點
{
XmlElement xe = (XmlElement)xn;//將子節(jié)點類型轉(zhuǎn)換為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");//保存。
成都創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的重慶網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
讀取
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Initialize.xml");
XmlNodeList nodeList = xmlDoc.SelectSingleNode("ComValue").ChildNodes;//獲取 節(jié)點的所有子節(jié)點
foreach (XmlNode xn in nodeList)//遍歷所有子節(jié)點
{
XmlElement xe = (XmlElement)xn;//將子節(jié)點類型轉(zhuǎn)換為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;
}
}
分享標(biāo)題:Csharp讀寫XML
瀏覽地址:http://fisionsoft.com.cn/article/jcggie.html