XML文檔的操作


<?xml version="1.0" encoding="utf-8"?>
<bookstore>
  <book ISBN="123">
    <title>數據一</title>
    <auhtor>TOM</auhtor>
  </book>
  <book ISBN="124">
    <title>數據二</title>
    <auhtor>Jerry</auhtor>
  </book>
  <book ISBN="nameTest">
    <title>藺宜忠前景展望</title>
    <author>藺宜忠</author>
  </book>
</bookstore>

            //輸出xml文件
            //XmlDocument xml = new XmlDocument();
            //xml.Load(Server.MapPath("App_Data/BookStore.xml"));
            //Response.Write(Server.HtmlEncode(xml.InnerXml));

            //輸出xml文檔,保存xml文件
            //XmlDocument doc = new XmlDocument();
            //string xml = "<?xml version=/"1.0/" encoding=/"utf-8/" ?><bookstore> <book ISBN=/"123/"> <title>數據一</title>  <auhtor>TOM</auhtor> </book>  <book ISBN=/"124/">    <title>數據二</title>   <auhtor>Jerry</auhtor>  </book></bookstore>";
            //doc.LoadXml(xml);
            //Response.Write(Server.HtmlEncode(doc.InnerXml));
            //string file = Server.MapPath("~/App_Data/book2.xml");
            //doc.Save(file);

            //添加一個xml節點
            //string file = Server.MapPath("~/App_Data/book2.xml");
            //XmlDocument doc = new XmlDocument();
            //doc.Load(file);
            //XmlElement book = doc.CreateElement("book");
            //book.SetAttribute("ISBN", "nameTest");
            //XmlElement title = doc.CreateElement("title");
            //title.InnerText = "藺宜忠自傳";
            //XmlElement author = doc.CreateElement("author");
            //author.InnerText = "藺宜忠";
            //book.AppendChild(title);
            //book.AppendChild(author);
            //XmlElement root = doc.DocumentElement;
            //root.AppendChild(book);
            //doc.Save(file);

            //刪除最後節點
            //string file = Server.MapPath("~/App_Data/book2.xml");
            //XmlDocument doc = new XmlDocument();
            //doc.Load(file);
            //XmlElement root = doc.DocumentElement;
            //XmlNode toRemoveNode=   root.LastChild;
            //root.RemoveChild(toRemoveNode);
            //doc.Save(file);

            //獲取某個節點
            //string file = Server.MapPath("~/App_Data/book2.xml");
            //XmlDocument doc = new XmlDocument();
            //doc.Load(file);
            //string isbn = "nameTest";
            //XmlNode root = doc.DocumentElement;
            //foreach (XmlNode child in root.ChildNodes)
            //{
            //    if (child.Attributes["ISBN"].Value == isbn)
            //    {
            //        //XmlNode title = child.ChildNodes[0];
            //        //XmlNode title = child.FirstChild;
            //        XmlNode title = child.SelectSingleNode("./title");
            //        Response.Write(title.InnerText);
            //    }
            //}
            //更新節點
            string file = Server.MapPath("~/App_Data/book2.xml");
            XmlDocument doc = new XmlDocument();
            doc.Load(file);
            string isbn = "nameTest";
            XmlNode root = doc.DocumentElement;
            foreach (XmlNode child in root.ChildNodes)
            {
                if (child.Attributes["ISBN"].Value == isbn)
                {
                    //XmlNode title = child.ChildNodes[0];
                    //XmlNode title = child.FirstChild;
                    XmlNode title = child.SelectSingleNode("./title");
                    title.InnerText = "藺宜忠前景展望";
                    Response.Write(title.InnerText);
                }
            }
            doc.Save(file);

 

 

 

 

 

 

 

 

 

 

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Xml.Linq;

using System.Linq;

 

namespace Learnmark.Website.Common

{

    public class cls_News

    {

        private string m_dataPath;

 

        public cls_News(string dataPath)

        {

            if (System.IO.File.Exists(dataPath) == true)

            {

                DataPath = dataPath;

            }

            else

            {

                DataPath = dataPath;

                RebuildNewsesDataSchema();

            }

        }

 

        public string DataPath

        {

            get

            {

                return m_dataPath;

            }

            set

            {

                m_dataPath = value;

            }

        }

 

        public bool AddNews(int newsType, string newsTitle, string newsDetails, string newsDate)

        {

            try

            {

                string newsSubTitle;

                if (newsTitle.Length > 18)

                {

                    newsSubTitle = newsTitle.Substring(0, 18) + "...";

                }

                else

                {

                    newsSubTitle = newsTitle;

                }

                XElement newses = XElement.Load(DataPath);

                XElement news = new XElement("News",

                    new XAttribute("ID", GetNextNewsID()),

                    new XAttribute("Type", newsType.ToString()),

                    new XAttribute("Title", newsTitle),

                    new XAttribute("DisTitle", newsSubTitle),

                    new XAttribute("Details", newsDetails),

                    new XAttribute("Date", newsDate),

                    new XAttribute("DateCreated", DateTime.Now.ToString("s")),

                    new XAttribute("DateUpdated", DateTime.Now.ToString("s"))

                    );

                newses.AddFirst(news);

                newses.Save(DataPath);

                return true;

            }

            catch

            {

                return false;

            }

        }

 

        public bool RebuildNewsesDataSchema()

        {

            try

            {

                XDocument newsDataSchema = new XDocument(

                    new XElement("Newes"));

                newsDataSchema.Save(DataPath);

                return true;

            }

            catch

            {

                return false;

            }

        }

 

        public int GetNextNewsID()

        {

            try

            {

                XElement newses = XElement.Load(DataPath);

                IEnumerable<int> IDs =

                    from el in newses.Elements("News")

                    let ID = (int)el.Attribute("ID")

                    orderby ID descending

                    select ID;

                int nowID = Convert.ToInt32(IDs.Max());

                return nowID + 1;

            }

            catch

            {

                return 1;

            }

        }

 

        public bool DeleteNews(int newsID)

        {

            try

            {

                XElement newses = XElement.Load(DataPath);

                var newsNeedDel =

                    from el in newses.Elements("News")

                    where (int)el.Attribute("ID") == newsID

                    select el;

                newsNeedDel.Remove();

                newses.Save(DataPath);

                return true;

            }

            catch

            {

                return false;

            }

        }

 

        public bool UpdateNews(int newsID,int newsType, string newsTitle, string newsDetails, string newsDate)

        {

            try

            {

                XElement newses = XElement.Load(DataPath);

                var newsInfo =

                    from el in newses.Elements("News")

                    where (int)el.Attribute("ID") == newsID

                    select el;

                newsInfo.Single().SetAttributeValue("Type", newsType.ToString());

                newsInfo.Single().SetAttributeValue("Title", newsTitle);

                newsInfo.Single().SetAttributeValue("Details", newsDetails);

                newsInfo.Single().SetAttributeValue("Date", newsDate);

                newsInfo.Single().SetAttributeValue("DateUpdated", DateTime.Now.ToString("s"));

                newses.Save(DataPath);

                return true;

            }

            catch

            {

                return false;

            }

        }

 

        public XElement GetNews(int newsID)

        {

            XElement newses = XElement.Load(DataPath);

            var newsInfo =

                from el in newses.Elements("News")

                where (int)el.Attribute("ID") == newsID

                select el;

            return newsInfo.Single();

        }

    }

}

 

 

 

********************

 

 

 

 

 

 

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using Learnmark.Website.Common;

 

namespace Learnmark.Website.WebUI.Components

{

    public partial class NewsDetails : System.Web.UI.UserControl

    {

        cls_News newsDetails;

        public string m_NewsTitle = string.Empty;

        public string m_NewsDetails = string.Empty;

        public string m_NewsDateUpdated = string.Empty;

        protected void Page_Load(object sender, EventArgs e)

        {

            newsDetails = new cls_News(Server.MapPath("~/App_Data/NewsData/News.xml"));

            if (Convert.ToString(Request.QueryString["ID"]) != "")

            {

                XElement news = newsDetails.GetNews(Convert.ToInt32(Request.QueryString["ID"]));

                m_NewsTitle = news.Attribute("Title").Value.ToString();

                m_NewsDetails = news.Attribute("Details").Value.ToString();

                m_NewsDateUpdated = news.Attribute("Date").Value.ToString();

            }

            lblTitle.Text = m_NewsTitle;

            lblDetails.Text = m_NewsDetails;

            lblDateUpdated.Text = m_NewsDateUpdated;

            Page.Title = m_NewsTitle + "訓,精英IT培訓解決方案提供商";

        }

    }

}

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章