FW: DOTNET Quiz 2011 - play with XML

DOTNET Quiz 2011 - play with XML

Following is the sample XML file named AllBooks.xml for various Computer books published by different publishers alongwith some book details. http://www.wmd-editor.com/


<?xml version="1.0" encoding="utf-8" ?>
<AllBooks>
  <Publishers>
    <Publisher PublisherName="MS Press">
      <Books>
        <Book Title="Microsoft SQL Server 2000 DTS Step by Step" Author="Carl Rabeler" Pages="450"></Book>
        <Book Title="Microsoft SQL Server 2005 Integration Services Step by Step" Author="Paul Turleyet" Pages="480"></Book>
        <Book Title="Microsoft SQL Server 2008 Step by Step" Author="Mike Hotek" Pages="800"></Book>       
      </Books>
    </Publisher>

    <Publisher PublisherName="APress">
      <Books>
        <Book Title="SQL Server Security Distilled" Author="Morris Lewis" Pages="352"></Book>
        <Book Title="SQL Server 2008 Query Performance Tuning Distilled" Author="Grant Fritchey" Pages="545"></Book>
      </Books>
    </Publisher>
  </Publishers>
</AllBooks>

Assume that in above xml we have unique publishers and Titles per Publisher. You are expected to write a static method called GetBook() with following conditions:

Programming language must be C#
Method should accept following three input parameters (1) String strXMLFilePath (2) String strPublisherName (3) String strTitle
Method should locate a <Book> in AllBooks.xml file as specified by strXMLFilePath on the basis of parameters strPublisherName and strTitle that are passed to it.
Method must return XmlNode type of object for a <Book> node when the values of parameters strPublisherName and strTitle match to PublisherName and Title respectively for one the books.
Method must be capable to find corresponding book even if the CASE of values of PublisherName and Title are not maintained when specified. In another words, method must locate the book “SQL Server Security Distilled” even if the value of the passed parameter is “sQl serVer seCuriTy diStilleD”
Number of statements in GetBook() should be as minimum as possible.
Use of LINQ is not allowed.
All participants are requested to provide explanation of code and the approach you have taken.

 

static void GetBook(string strXMLFilePath, string strPublisherName, string strTitle)
{
 XmlDocument xd = new XmlDocument();
 xd.Load(strXMLFilePath);
 XmlNode xn = xd.SelectSingleNode("/AllBooks/Publishers/Publisher[translate(@PublisherName,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')='" + strPublisherName.ToUpper() + "']/Books/Book[translate(@Title,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')='" + strTitle.ToUpper() + "']", null);    
 Console.WriteLine(xn.OuterXml);
}

posted @ 2011-01-03 22:23  AOT  阅读(223)  评论(0编辑  收藏  举报