1

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 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>

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)

2
  • I disagree on "should be populated as 2 records". ISBN-10 and ISBN-13 are two different things, and they should be two different columns in your database (or two different fields in your objects). Duplicating the record is a classic denormalization that leads to data integrity problems down the road (what if you search for the book by its ISBN-10, change it, then later search it by its ISBN-13?) Commented Jan 7, 2011 at 16:33
  • I think u got me wrong....There are 2 ISBN13 and ISBN10 in the second set of Data , there are different columns for ISBN 10 & isbn 13 so those have to be populated as two records with same title,author and everthing with but with diff ISBN10 & ISBN13 since there are 2 for each Commented Jan 7, 2011 at 16:38

1 Answer 1

1

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");  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.