Stream,StreamReader,StreamWriter
读写声明的时候可直接加路径StreamReader读入流: StreamReader sr = new StreamReader("D://a.xml"); string result = sr.ReadToEnd();
StreamWriter写入流:using (StreamWriter sw = new StreamWriter("D://b.txt",true))//true表示原文件后追加Append(true)或覆盖原文件(false) { sw.Write("/n<a name=/"pp/">username='www'</a>"); sw.Flush(); sw.Close(); //sw.Dispose();
StreamReader sr = new StreamReader("D://b.txt"); string result = sr.ReadToEnd(); MessageBox.Show(result); sr.Close(); //sr.Dispose(); }读完或写入完毕后记得关闭(sw.Close()),否则容易资源正被占用错误!
Stream流:可以直接使用,或者通过StreamReader的BaseStream属性获得Stream sr=sr.BaseStream;
应用:读写配置文件private void ReadConfig(){ string ret = ""; StreamReader streamReader = new StreamReader("D://a.config"); XmlDocument xdoc = new XmlDocument();
Stream stream = streamReader.BaseStream; xdoc.Load(stream); XmlNodeList neXml; neXml = xdoc.SelectNodes("/config/dir"); foreach (XmlNode xn in neXml) { string time = xn.Attributes["name"].Value; ret += time.ToString() + "/n"; } stream.Close();}
<?xml version="1.0" encoding="UTF-8"?><config><dir name="20091201130000"><file>Coverag_</file></dir><dir name="20091201120000"><file>Coverag_</file></dir><dir name="20091201110000"><file>Coverag_</file></dir><dir name="20091201100000"><file>Coverag_</file></dir></config>
/// 获取渲染的时间列表 /// </summary> /// <returns></returns> public List<string> GetTimeStamp() { List<string> ret = new List<string>(); string st=string.Empty;
string url = ConfigurationManager.AppSettings["TimeStampURL"]; HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url); httpRequest.Method = "GET"; httpRequest.Timeout = 100000; HttpWebResponse httpResponse = null; try { //获取HTTP响应 httpResponse = (HttpWebResponse)httpRequest.GetResponse(); Stream stream = httpResponse.GetResponseStream();
//StreamReader sr = new StreamReader(stream1); //st = sr.ReadToEnd();
//Stream stream = sr.BaseStream; //解析返回的字符串 XmlDocument xdoc = new XmlDocument(); xdoc.Load(stream); XmlNodeList neXml; neXml = xdoc.SelectNodes("/config/dir"); foreach (XmlNode xn in neXml) { string time = xn.Attributes["name"].Value; ret.Add(time); }
//stream.Position = 0; //StreamReader sr = new StreamReader(stream); //st = sr.ReadToEnd();
stream.Close(); httpResponse.Close(); } catch(Exception ex) { LogLib.LogAccess.Log(ex.ToString()); if (httpResponse != null) httpResponse.Close(); } if (ret.Count > 0) { LogLib.LogAccess.Log("timestamp success!stream:" + st); LogLib.LogAccess.Log("timestamp list:" + ret[0].ToString()); } else { LogLib.LogAccess.Log("timestamp failed!stream:"+st); //LogLib.LogAccess.Log("获取渲染时间列表失败!" + ret[0].ToString()); } return ret; }
Silverlight类中的读入方式。LINQ查询
/// <summary> /// 取得查询条件列表 /// </summary> /// <returns></returns> public static List<NeZh> CreateNeList() { Uri fileUri = new Uri("/StMap;component/data/NeConfig.xml", System.UriKind.Relative); System.Windows.Resources.StreamResourceInfo streamInfo = System.Windows.Application.GetResourceStream(fileUri);
XmlReader reader = XmlReader.Create(new StreamReader(streamInfo.Stream)); XDocument postdoc = XDocument.Load(reader);
List<NeZh> ne = new List<NeZh>(); List<Item> item = new List<Item>(); List<DisplayItem> displayitem = new List<DisplayItem>(); List<NeConfig> nec = new List<NeConfig>(); IEnumerable<XElement> neXml; IEnumerable<XElement> neConfigXml; IEnumerable<XElement> neShow; IEnumerable<XElement> neConfigShow; IEnumerable<XElement> neDisplay; IEnumerable<XElement> neConfigDisplay;
neXml = from element in postdoc.Descendants(XName.Get("Ne")) select element; foreach (XElement xn in neXml) { NeZh n = new NeZh(); nec = new List<NeConfig>(); string calss = xn.Attribute("class").Value; neConfigXml = from element in xn.Descendants(XName.Get("NeConfig")) select element; foreach (XElement xn1 in neConfigXml) { NeConfig nc = new NeConfig(); item = new List<Item>(); displayitem = new List<DisplayItem>(); string lai = xn1.Attribute("来源").Value; string zh1 = xn1.Attribute("zh").Value; string sql = xn1.Element(XName.Get("SQL")).Value; neShow = from element in xn1.Descendants(XName.Get("Show")) select element; foreach (XElement xShow in neShow) { neConfigShow = from element in xShow.Descendants(XName.Get("Item")) select element; foreach (XElement xn2 in neConfigShow) { string zh = xn2.Attribute("ZH").Value; string eg = xn2.Attribute("EG").Value; Item th = new Item(); th.ZH = zh; th.EG = eg; item.Add(th); } } neDisplay = from element in xn1.Descendants(XName.Get("Display")) select element; foreach (XElement Display in neDisplay) { neConfigDisplay = from element in Display.Descendants(XName.Get("Item")) select element; foreach (XElement xn3 in neConfigDisplay) { string zh = xn3.Attribute("ZH").Value; string eg = xn3.Attribute("EG").Value; DisplayItem th = new DisplayItem(); th.ZH = zh; th.EG = eg; displayitem.Add(th); } } nc.Item = item; nc.DisplayItem = displayitem; nc.Sql = sql; nc.NeSystem = (NeSystem)ObjectType.Parse(typeof(NeSystem), lai, true); nc.ZH = zh1; nec.Add(nc); } n.NeConfig = nec; n.Netype = (ObjectType)ObjectType.Parse(typeof(ObjectType), calss, true); ne.Add(n); } return ne; }
xml文件:<?xml version="1.0" encoding="utf-8" ?><Config> <Ne class="MSC"> <NeConfig 来源="RMSystem" zh="配置资源"> <SQL> <![CDATA[ SELECT uuid, localspc, msctype, mscid, nativespc, objclass, mscno, switch_capacity, e1_capacity, link64k_capacity, iwf_capacity, iwf_modnum, mscbhca, vlrcapacity, msc_freband, link2m_capacity, stm1_capacity, detach_time, auth_event, auth_interval, tmsirallocinfo, repagingtimes, repagingintval, paging_mode, purgeinterval, TO_CHAR (purgetime, 'yyyy-mm-dd hh24:mi:ss') purgetime, entity_id, omcuuid, softversion, mgmtipaddrlist, hardwareversion, hardwareplatform, confirmed, cityuuid, nodedesc, TO_CHAR (online_time, 'yyyy-mm-dd hh24:mi:ss') online_time, vendorname, provinceuuid, vendor_id, necode, prefectureuuid, roomuuid, operationstatus, countyuuid, labelcn, object_rdn, objname, ou_id, source_name, fm_rdn, fm_uuid, TO_CHAR (update_time, 'yyyy-mm-dd hh24:mi:ss') update_time,uuid int_id FROM a_r_msc {1} WHERE 1=1 and {0} ]]> </SQL> <Show> <Item ZH="网元名称" EG="Objname"></Item> </Show> <Display> <Item ZH="网元名称" EG="Objname"></Item> </Display> </NeConfig> <NeConfig 来源="PMSystem" zh="性能"> <SQL> <![CDATA[ select to_char(first_result,'yyyy-mm-dd hh24:mi:ss') first_result, msc_name, round(att_calls_sys,2) att_calls_sys, round(suc_answ_sys,2) suc_answ_sys, round(suc_answ_rate,2) suc_answ_rate, round(unsuc_answ_sys,2) unsuc_answ_sys, round(unsuc_answ_rate,2) unsuc_answ_rate, round(ms_ms_call_att,2) ms_ms_call_att, round(ms_ms_call_rate,2) ms_ms_call_rate, round(fix_for_call_att,2) fix_for_call_att, round(fix_for_call_rate,2) fix_for_call_rate, round(ms_fix_call_att,2) ms_fix_call_att, round(ms_fix_call_rate,2) ms_fix_call_rate, round(fix_ms_call_att,2) fix_ms_call_att, round(fix_ms_call_rate,2) fix_ms_call_rate, round(orig_call_att,2) orig_call_att, round(orig_call_rate,2) orig_call_rate, round(income_call_att,2) income_call_att, round(income_call_rate,2) income_call_rate, round(term_call_att,2) term_call_att, round(term_call_rate,2) term_call_rate, round(outgo_call_att,2) outgo_call_att, round(outgo_call_rate,2) outgo_call_rate, round(covered_minutes,2) covered_minutes, int_id, omc_id, omc_int_id from gis.A_P_MSC_NE {1} where 1=1 and {0} ]]> </SQL> <Show> <Item ZH="时间" EG="First_result" /> <Item ZH="MSC名称" EG="Msc_name" /> <Item ZH="试呼次数" EG="Att_calls_sys" /> <Item ZH="应答次数" EG="Suc_answ_sys" /> <Item ZH="接通率" EG="Suc_answ_rate" /> <Item ZH="无应答次数" EG="Unsuc_answ_sys" /> <Item ZH="无应答比例" EG="Unsuc_answ_rate" /> </Show> <Display> <Item ZH="时间" EG="First_result" /> <Item ZH="MSC名称" EG="Msc_name" /> <Item ZH="试呼次数" EG="Att_calls_sys" /> <Item ZH="应答次数" EG="Suc_answ_sys" /> <Item ZH="接通率" EG="Suc_answ_rate" /> <Item ZH="无应答次数" EG="Unsuc_answ_sys" /> <Item ZH="无应答比例" EG="Unsuc_answ_rate" /> </Display> </NeConfig> </Ne>