zoukankan      html  css  js  c++  java
  • XML操作

    XmlDocument document = new XmlDocument();
    document.Load("Data\books.xml");

    List<Book> bookList = new List<Book>();
    foreach (XmlElement book in document.DocumentElement.ChildNodes)
    {
        string isbn = book.GetAttribute("ISBN").ToString();//属性
        string genre = book.GetAttribute("genre").ToString();//属性
        string title = book.SelectSingleNode("title").InnerText;//节点
        string author = book.SelectSingleNode("author").InnerText;
        string price = book.SelectSingleNode("price").InnerText;
        Book bookInfo = new Book();
        bookInfo.ISBN = isbn;
        bookInfo.Genre = genre;
        bookInfo.Title = title;
        bookInfo.Author = author;
        bookInfo.Price = Convert.ToDouble(price);
        bookList.Add(bookInfo);
    }

    //根据特定的值获取节点

    XmlNode targetNode = document.SelectSingleNode("bookstore/book[@ISBN='2-3631-4']");//获得目标节点
    //以下两种写法效果一样
    XmlNode targetNode = document.SelectSingleNode("/bookstore/book/title[text()='CS从入门到精通']");//获得目标节点
    XmlNode targetNode = document.SelectSingleNode("/bookstore/book/title[.='CS从入门到精通']");//获得目标节点

    book.xml

    <?xml version="1.0" encoding="gb2312"?>
    <bookstore>
      <book genre="李赞红" ISBN="2-3631-4">
        <title>CS从入门到精通</title>
        <author>候捷</author>
        <price>58.3</price>
      </book>
    </bookstore>


     

  • 相关阅读:
    TweenLite简单运用
    nodejs 重定向 (redirect + writeHead(Location))
    Nodejs Web模块( readFile 根据请求跳转到响应html )
    Express框架(http服务器 + 路由)
    AI 学习路线
    implicitly_wait()隐式等待
    Python 爬虫基础Selenium
    Selenium2+python自动化15-select下拉框
    python selenium while 循环
    jupyter notebook修改默认路径和浏览器
  • 原文地址:https://www.cnblogs.com/pnljs/p/3443783.html
Copyright © 2011-2022 走看看