C#.Net操作XML
1,读取Rss订阅XML文件:读取线上的XML
usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Net;usingSystem.Text;namespaceXmlTest.com{///<summary>///获得线上的Rss信息/XML格式的</br>///订阅信息///</summary>publicclassLoadOnlineXml{privatereadonlystringrssPath;privatereadonlystringencoding;///<summary>//////</summary>///<paramname="rssPath">Rss地址,如:http://news.163.com/special/00011K6L/rss_newstop.xml</param>///<paramname="encoding">此Rss的编码格式</param>publicLoadOnlineXml(stringrssPath,stringencoding){this.rssPath=rssPath;this.encoding=encoding;}///<summary>///打印目标Rss///</summary>publicvoidwriteRss(){WebClientweb=newWebClient();Streamstream=web.OpenRead(this.rssPath);StreamReaderreader=newStreamReader(stream,Encoding.GetEncoding(this.encoding));stringrssText=reader.ReadToEnd();Console.WriteLine(rssText);stream.Close();reader.Close();}}}
2,读取XML
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Xml;namespaceXmlTest.com{///<summary>///读取XML///</summary>publicclassReaderXml{privatereadonlystringxmlPath;///<summary>//////</summary>///<paramname="xmlPath">XML的地址</param>publicReaderXml(stringxmlPath){this.xmlPath=xmlPath;}///<summary>///读XML信息///</summary>publicvoidreadXML(){stringstr="";XmlReaderreader=XmlReader.Create(this.xmlPath);inti=0,j=0;while(reader.Read()){if(reader.NodeType==XmlNodeType.Element){str+="节点名:"+reader.Name+Environment.NewLine;str+=",节点类型:"+reader.NodeType+Environment.NewLine;str+=",节点深度:"+reader.Depth+Environment.NewLine;if(reader.HasAttributes)//是否存在属性{for(i=0,j=reader.AttributeCount;i<j;i+=1){reader.MoveToAttribute(i);str+="属性:"+reader.Name+"值为:"+reader.Value+Environment.NewLine;}}}elseif(reader.NodeType==XmlNodeType.Text){if(reader.HasValue)//是否存在文本值{str+=",节点的文本值:"+reader.Value+Environment.NewLine;}}else{}}reader.Close();Console.WriteLine(str);}}}
3,创建一个XML文件
usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Xml;namespaceXmlTest.com{///<summary>///模拟没有xml的时候重建一个XML文件///</summary>publicclassXmlWriterDefault{privatereadonlystringxmlpath;///<summary>//////</summary>///<paramname="xmlpath">xml路径</param>publicXmlWriterDefault(stringxmlpath){this.xmlpath=xmlpath;}///<summary>///编写一个基本的XML文件///</summary>///<paramname="encoding">编码格式</param>publicvoidwriterDefault(Encodingencoding){if(!File.Exists(this.xmlpath)){XmlWriterSettingswriter=newXmlWriterSettings();writer.Encoding=encoding;using(XmlWriterw=XmlWriter.Create(this.xmlpath,writer)){w.WriteStartElement("fristElement");w.WriteAttributeString("name","Ainy");w.WriteAttributeString("gender","male");w.WriteEndElement();w.Flush();}}}}}
4 , 核心操作
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Xml;namespaceXmlTest.com{///<summary>///对XML信息的增,删,改///</summary>publicclassXmlMainHandler{privatereadonlystringxmlPath;///<summary>//////</summary>///<paramname="xmlPath">目标XML的地址</param>publicXmlMainHandler(stringxmlPath){this.xmlPath=xmlPath;}///<summary>///增加一个属性///</summary>///<paramname="name">属性名称</param>///<paramname="value">属性值</param>publicvoidaddAttribute(stringname,stringvalue,stringtargetName){XmlDocumentxdoc=newXmlDocument();xdoc.Load(this.xmlPath);foreach(XmlElementxElementinxdoc.DocumentElement.ChildNodes){foreach(XmlElementxEinxElement.ChildNodes){Console.WriteLine("属性值:"+xE.GetAttribute("name"));if(xE.GetAttribute("name")==targetName){XmlAttributexA=xdoc.CreateAttribute(name);xA.Value=value;xE.Attributes.Append(xA);break;}}}xdoc.Save(this.xmlPath);}///<summary>///删除一个属性///</summary>///<paramname="AttributeName"></param>publicvoidremoveAttribute(stringAttributeName){XmlDocumentxdoc=newXmlDocument();xdoc.Load(this.xmlPath);XmlNodetargetNote=xdoc.SelectSingleNode(@"friends/friend/describe");//!important:第一次找到的friends/friend/describe,如果没有找到,返回nullif(targetNote!=null){//定义一个属性if(targetNote.Attributes[AttributeName]!=null){targetNote.Attributes.Remove(targetNote.Attributes[AttributeName]);}else{Console.WriteLine("此节点没有'{0}'属性",AttributeName);}}else{Console.WriteLine("没有找到friends/friend/describe请检查");}xdoc.Save(this.xmlPath);}///<summary>///修改一个属性///</summary>///<paramname="AttributeName"></param>///<paramname="value"></param>publicvoideditorAttribute(stringAttributeName,stringvalue){XmlDocumentxdoc=newXmlDocument();xdoc.Load(this.xmlPath);XmlNodeListtargetList=xdoc.SelectNodes(@"friends/friend/base");foreach(XmlNodetargetintargetList){if(target.Attributes[AttributeName]!=null){if(target.Attributes[AttributeName].Value=="eisa"){target.Attributes[AttributeName].Value="eisa-"+value;target.InnerText="XXX";//加一个Textxdoc.Save(this.xmlPath);return;}}}Console.WriteLine("修改属性失败");}}}
附件 : XML friends.xml
<?xmlversion="1.0"encoding="utf-8"?><friends><friend><basename="eisa-snow"tel="135244&&&"gender="female">XXX</base><describe>my---</describe></friend><friend><basename="kawen"tel="13533449078"gender="male"亲密度="80%"/><describe>my****</describe></friend></friends>
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。