新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

C#.Net操作XML-创新互联

1,读取Rss订阅XML文件:读取线上的XML

定南网站制作公司哪家好,找创新互联建站!从网页设计、网站建设、微信开发、APP开发、响应式网站设计等网站项目制作,到程序开发,运营维护。创新互联建站从2013年创立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联建站
 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 ****
  

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站栏目:C#.Net操作XML-创新互联
文章出自:http://www.wjwzjz.com/article/dessed.html
在线咨询
服务热线
服务热线:028-86922220
TOP