日期:2014-05-17 浏览次数:20823 次
using System; using System.Collections.Generic; using System.Linq; using System.Xml; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string xml = @"<Seriesport name=""SeriesportSetting""> <portnumber>COM5</portnumber> <baudrate>57600</baudrate> <databits>8</databits> <paritybit>无</paritybit> <stopbit>2</stopbit> </Seriesport> "; XmlDocument xdoc = new XmlDocument(); xdoc.LoadXml(xml); string portnumber = xdoc.ChildNodes[0].ChildNodes[0].InnerText; string baudrate = xdoc.ChildNodes[0].ChildNodes[1].InnerText; Console.WriteLine(portnumber); Console.WriteLine(baudrate); } } }
------解决方案--------------------
XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(@"c:\test.xml"); Console.WriteLine(xmlDoc.DocumentElement["portnumber"].InnerText); Console.WriteLine(xmlDoc.DocumentElement["baudrate"].InnerText);