剑轩

xml添加,添加,删除,修改

电脑版发表于:2020/10/23 12:09

代码如下:

public ActionResult Index()
        {
            XDocument document = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"));
            XNamespace ns = "https://www.tnblog.net";
            XElement root = new XElement(ns + "svg");
            root.Add(
                    new XAttribute("xmlns", "https://www.tnblog.net"),
                    new XElement(ns + "bookname", "我记得有一个人"),
                    new XElement(ns + "bookdesc", "永远留在我心中")
                    );
            document.Add(root);

            string uri = Server.MapPath("~/xml/MyTest.xml");
            document.Save(uri);
            return View();
        }



        public ActionResult Remove()
        {
            string uri = Server.MapPath("~/xml/boos2.xml");

            var root = XElement.Load(uri);

            XElement book = root.Element("books").Elements("book").Where(a => a.Attribute("bno").Value == "b001").FirstOrDefault();
            book.Remove();

            root.Save(uri);

            return View();
        }

        public ActionResult Update()
        {
            string uri = Server.MapPath("~/xml/boos2.xml");

            var root = XElement.Load(uri);


            XElement book = root.Element("books").Elements("book").Where(a => a.Attribute("bno").Value == "b001").FirstOrDefault();
            book.ReplaceNodes(new XElement("sister", "子菁"),
                new XElement("height", "180kg"));

            root.Save(uri);

            return View();
        }


        //读取书的详情
        public ActionResult Details(string bid)
        {
            string uri = Server.MapPath("~/xml/boos2.xml");
            XElement book = XElement.Load(uri).Element("books").Elements("book").Where(a => a.Attribute("bno").Value == bid).FirstOrDefault();

            Response.Write("书籍编号:" + bid + "<br/>");
            Response.Write("书籍名称:" + book.Element("bookname").Value + "<br/>");
            Response.Write("书籍价格:" + book.Element("bookprice").Value + "<br/>");

            var config = XElement.Load(Server.MapPath("~/Web.config"));
            Response.Write("读取config连接字符串:" + config.Element("connectionStrings").Element("add").Attribute("connectionString").Value + "<br/>");


            return View();
        }

        public ActionResult ReadXML()
        {
            string uri = Server.MapPath("~/xml/boos2.xml");
            //读取一个xml
            XElement root = XElement.Load(uri);

            var book = root.Element("books").Elements("book");

            foreach (var item in book)
            {
                string bookname = item.Element("bookname").Value;
                string bid = item.Attribute("bno").Value;
                Response.Write("<div><a href='/home/details/" + bid + "'>" + bookname + "</a></div>");
            }

            return View();
        }


关于TNBLOG
TNBLOG,技术分享。技术交流:群号677373950
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
{{item.replyName}}
{{item.content}}
{{item.time}}
{{subpj.replyName}}
@{{subpj.beReplyName}}{{subpj.content}}
{{subpj.time}}
猜你喜欢