排名
                
                
                    6
                
            
                    文章
                
                
                    6
                
            
                    粉丝
                
                
                    16
                
            
                    评论
                
                
                    8
                
            
            {{item.articleTitle}}
            
    {{item.blogName}} : {{item.content}}
        
            ICP备案  :渝ICP备18016597号-1
        
        
            网站信息:2018-2025TNBLOG.NET
        
        
            技术交流:群号656732739
        
        
            联系我们:contact@tnblog.net
        
        
            公网安备:
50010702506256
        
    
50010702506256
        
        
            欢迎加群交流技术
        
    
    
    分类:
    .NET
代码如下:
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();
        }欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)
评价