大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
1,读取Rss订阅XML文件:读取线上的XML
创新互联主营苏家屯网站建设的网络公司,主营网站建设方案,手机APP定制开发,苏家屯h5小程序定制开发搭建,苏家屯网站营销推广欢迎苏家屯等地区企业咨询
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; namespace XmlTest.com { ////// 获得线上的Rss信息 / XML格式的 /// 订阅信息 /// public class LoadOnlineXml { private readonly string rssPath; private readonly string encoding; ////// /// /// Rss地址 , 如 : http://news.163.com/special/00011K6L/rss_newstop.xml /// 此Rss的编码格式 public LoadOnlineXml(string rssPath , string encoding ) { this.rssPath = rssPath; this.encoding = encoding; } ////// 打印目标Rss /// public void writeRss() { WebClient web = new WebClient(); Stream stream = web.OpenRead(this.rssPath); StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(this.encoding)); string rssText = reader.ReadToEnd(); Console.WriteLine(rssText); stream.Close(); reader.Close(); } } }
2,读取XML
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; namespace XmlTest.com { ////// 读取XML /// public class ReaderXml { private readonly string xmlPath; ////// /// /// XML的地址 public ReaderXml(string xmlPath) { this.xmlPath = xmlPath; } ////// 读XML信息 /// public void readXML() { string str = ""; XmlReader reader = XmlReader.Create( this.xmlPath ); int i = 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; } } } else if ( reader.NodeType == XmlNodeType.Text ) { if (reader.HasValue) //是否存在文本值 { str += ", 节点的文本值 : " + reader.Value + Environment.NewLine; } } else { } } reader.Close(); Console.WriteLine(str); } } }
3,创建一个XML文件
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Xml; namespace XmlTest.com { ////// 模拟没有xml的时候重建一个XML文件 /// public class XmlWriterDefault { private readonly string xmlpath; ////// /// /// xml路径 public XmlWriterDefault(string xmlpath) { this.xmlpath = xmlpath; } ////// 编写一个基本的XML文件 /// /// 编码格式 public void writerDefault( Encoding encoding ) { if ( !File.Exists( this.xmlpath ) ) { XmlWriterSettings writer = new XmlWriterSettings(); writer.Encoding = encoding; using( XmlWriter w = XmlWriter.Create( this.xmlpath , writer )) { w.WriteStartElement("fristElement"); w.WriteAttributeString("name" , "Ainy"); w.WriteAttributeString("gender", "male"); w.WriteEndElement(); w.Flush(); } } } } }
4 , 核心操作
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; namespace XmlTest.com { ////// 对XML信息的增,删,改 /// public class XmlMainHandler { private readonly string xmlPath; ////// /// /// 目标XML的地址 public XmlMainHandler(string xmlPath) { this.xmlPath = xmlPath; } ////// 增加一个属性 /// /// 属性名称 /// 属性值 public void addAttribute(string name, string value , string targetName ) { XmlDocument xdoc = new XmlDocument(); xdoc.Load(this.xmlPath); foreach (XmlElement xElement in xdoc.DocumentElement.ChildNodes) { foreach (XmlElement xE in xElement.ChildNodes) { Console.WriteLine("属性值 : " + xE.GetAttribute("name")); if (xE.GetAttribute("name") == targetName) { XmlAttribute xA = xdoc.CreateAttribute(name); xA.Value = value; xE.Attributes.Append(xA); break; } } } xdoc.Save(this.xmlPath); } ////// 删除一个属性 /// /// public void removeAttribute(string AttributeName) { XmlDocument xdoc = new XmlDocument(); xdoc.Load(this.xmlPath); XmlNode targetNote = xdoc.SelectSingleNode(@"friends/friend/describe"); //!important : 第一次找到的friends/friend/describe , 如果没有找到,返回null if (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); } ////// 修改一个属性 /// /// /// public void editorAttribute(string AttributeName, string value) { XmlDocument xdoc = new XmlDocument(); xdoc.Load(this.xmlPath); XmlNodeList targetList = xdoc.SelectNodes(@"friends/friend/base"); foreach (XmlNode target in targetList) { if (target.Attributes[AttributeName] != null) { if (target.Attributes[AttributeName].Value == "eisa") { target.Attributes[AttributeName].Value = "eisa-" + value; target.InnerText = "XXX"; //加一个Text xdoc.Save(this.xmlPath); return; } } } Console.WriteLine("修改属性 失败"); } } }
附件 : XML friends.xml
XXX my --- my ****