I need to parse an xml File in C#...The File will look some thing like this....
- <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 Ha开发者_如何学JAVAlf-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>
The data between tags book is one record. Now the second record contains two ISBN's so such kind of data should be populated as 2 records in the table(with everything same but different ISBN's)
Use xsd.exe
! You can take a XML sample file, run xsd.exe
on it twice (first to derive a XML schema from XML file, then to create a C# class from that schema) and you'll get a C# class which should be able to deserialize this XML into a C# object.:
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
The xsd.exe
utility is part of the Microsoft Windows SDK currently at v7.1 which you can download for free from here: http://msdn.microsoft.com/en-us/windows/bb980924
Now with this class in hand, you should be able to write something like:
XmlSerializer ser = new XmlSerializer(typeof(book));
var result = ser.Deserialize(@"C:\yourxmlfile.xml");
精彩评论