解析在C#
问题描述:
一个XML文件,我需要在C#来解析XML文件...该文件将看一些这样的事....解析在C#
- <book>
<rank>1</rank>
<list_name>Chapter Books</list_name>
<bestsellers_date>2010-12-26</bestsellers_date>
<published_date>2011-01-09</published_date>
<weeks_on_list>11</weeks_on_list>
<rank_last_week>0</rank_last_week>
<asterisk>0</asterisk>
<dagger>0</dagger>
- <book_details>
- <book_detail>
<title>THE LOST HERO</title>
<description>A return to Camp Half-Blood and semi-divine characters old and new.</description>
<contributor>by Rick Riordan</contributor>
<author>Rick Riordan</author>
<contributor_note />
<price>18.99</price>
<age_group>Ages 10 and up</age_group>
<publisher>Disney-Hyperion</publisher>
</book_detail>
</book_details>
- <isbns>
- <isbn>
<isbn13>9781423113393</isbn13>
<isbn10>142311339X</isbn10>
</isbn>
</isbns>
- <reviews>
- <review>
<book_review_link />
<first_chapter_link />
<sunday_review_link />
<article_chapter_link>http://artsbeat.blogs.nytimes.com/2010/06/21/the-world-of-percy-jackson-lives-on-in-the-lost-hero/</article_chapter_link>
</review>
</reviews>
</book>
- <book>
<rank>2</rank>
<list_name>Chapter Books</list_name>
<bestsellers_date>2010-12-26</bestsellers_date>
<published_date>2011-01-09</published_date>
<weeks_on_list>2</weeks_on_list>
<rank_last_week>0</rank_last_week>
<asterisk>0</asterisk>
<dagger>0</dagger>
- <book_details>
- <book_detail>
<title>THE GIFT</title>
<description>A sister and brother flex their new powers; a Witch and Wizard book.</description>
<contributor>by James Patterson and Ned Rust</contributor>
<author>James Patterson and Ned Rust</author>
<contributor_note />
<price>17.99</price>
<age_group>Ages 10 and up</age_group>
<publisher>Little, Brown</publisher>
</book_detail>
</book_details>
- <isbns>
- <isbn>
<isbn13>9780316036252</isbn13>
<isbn10>0316036250</isbn10>
</isbn>
- <isbn>
<isbn13>9780316122214</isbn13>
<isbn10>0316122211</isbn10>
</isbn>
</isbns>
- <reviews>
- <review>
<book_review_link />
<first_chapter_link />
<sunday_review_link />
<article_chapter_link />
</review>
</reviews>
</book>
标签的书之间的数据是一个记录。现在第二个记录包含两个ISBN,所以这样的数据应填充为表格中的2条记录(所有内容相同但不同的ISBN)
答
使用xsd.exe
!您可以使用XML示例文件,在其上运行xsd.exe
两次(首先从XML文件派生XML模式,然后从该模式创建C#类),然后您将得到一个C#类,该类应该能够反序列化此XML成C#对象:
C:\> xsd.exe (your-xml-file).xml -- this generates a (your-xml-file).xsd file
C:\> xsd.exe (your-XSD-file).xsd /C -- this generates the C# class from the XSD
的xsd.exe
程序是Microsoft Windows SDK的一部分,目前V7.1,你可以从这里免费下载:这个类在手http://msdn.microsoft.com/en-us/windows/bb980924
现在,你应该可以写下如下内容:
XmlSerializer ser = new XmlSerializer(typeof(book));
var result = ser.Deserialize(@"C:\yourxmlfile.xml");
我不同意“应该填充为2条记录”。 ISBN-10和ISBN-13是两个不同的东西,它们应该是数据库中的两个不同的列(或者对象中的两个不同的字段)。复制该记录是一种经典的反规范化,导致数据完整性问题在后面(如果您通过ISBN-10搜索该书,更改它,然后再通过其ISBN-13搜索它) – tdammers 2011-01-07 16:33:30
我想你让我错误......在第二组数据中有2个ISBN13和ISBN10,对于ISBN 10和13有不同的列,所以那些必须填充为具有相同标题,作者和具有不同ISBN10的两个记录, ISBN13,因为每个 – 2011-01-07 16:38:10