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>


     

  • 相关阅读:
    BZOJ 1631 Cow Party
    BZOJ 1927 星际竞速
    BZOJ 4059 Non-boring sequences
    BZOJ 1562 变换序列
    BZOJ 4417 超级跳马
    484586
    背板问题之满包问题
    对01背包路径的记录
    带权值的图 BFS
    漫步校园 杭电1428
  • 原文地址:https://www.cnblogs.com/pnljs/p/3443783.html
Copyright © 2011-2022 走看看