gpt4 book ai didi

c# - 如何解析这部分XML

转载 作者:太空宇宙 更新时间:2023-11-03 19:03:44 25 4
gpt4 key购买 nike

我是 XML 的新手,对解析此类文件有疑问:

XML 文档:

<?xml version="1.0" encoding="UTF-8"?>
<ISBNdb server_time="2005-07-29T02:41:22">
<BookList total_results="1" page_size="10" page_number="1" shown_results="1">
<BookData book_id="law_and_disorder" isbn="0210406240">
<Title>Law and disorder</Title>
<TitleLong>Law and disorder: law enforcement in television network news</TitleLong>
<AuthorsText>V. M. Mishra</AuthorsText>
<PublisherText publisher_id="asia_pub_house">New York: Asia Pub. House, c1979.</PublisherText>
<Details dewey_decimal="302.2/3"
dewey_decimal_normalized="302.23"
lcc_number="PN4888"
language="eng"
physical_description_text="x, 127 p. ; 22 cm."
edition_info=""
change_time="2004-10-19T23:52:56"
price_time="2005-07-29T02:06:41" />
</BookData>
</BookList>
</ISBNdb>

我将如何获得 ISBN 编号 <BookData book_id="law_and_disorder" isbn="0210406240">从文件的这一部分?下面是我用来解析的 C#:

C#解析代码

 XmlNodeList nodes = doc.DocumentElement.SelectNodes("/ISBNdb/BookList");
List<Book> books = new List<Book>();

foreach (XmlNode node in nodes)
{
Book book = new Book();

book.author = node.SelectSingleNode("BookData/AuthorsText").InnerText;
book.title = node.SelectSingleNode("BookData/Title").InnerText;
book.ISBN = node.SelectSingleNode("BookData").LastChild.ToString();

books.Add(book);

addInfo(book.author, book.title, book.ISBN);
}

我知道有不同的“节点”,例如“标题”和“详细信息”。但是,我不明白如何从节点“标签”本身获取信息。非常感谢任何澄清,并随时纠正我不正确的“定义”

谢谢!

最佳答案

book.ISBN = node.SelectSingleNode("BookData").Attributes["isbn"].Value;

isbn 是 BookData 的一个属性,而不是 child (即 Title)

关于c# - 如何解析这部分XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31997449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com