2009-08-24 63 views
0

幾個星期前我剛開始學習Visual Studio,一切都在我頭上。你能否給我一份我用來格式化日期欄和價格欄的代碼副本?我似乎也沒有得到gridview顯示覆選框。我堅持幫助。我正在使用visual studio 2008,並希望從xml文件中取出ROOM_COST, DATE_TO, ROOM_TYPE, LOCATION, SMOKING, RESTAURANT AND NO_OF_PEOPLE COLUMNS。用戶現在應該能夠選擇哪個滿足他們的要求。這裏是我的XML文件和我的LINQ到XML查詢的副本。從xml中檢索數據,關於用戶輸入和用戶從輸入列表中選擇

<?xml version="1.0" standalone="yes"?> 
<NewDataSet> 
    <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop"> 
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"> 
     <xs:complexType> 
     <xs:choice minOccurs="0" maxOccurs="unbounded"> 
      <xs:element name="Table"> 
      <xs:complexType> 
       <xs:sequence> 
       <xs:element name="ROOM_ID" msprop:OraDbType="111" type="xs:short" minOccurs="0" /> 
       <xs:element name="SMOKING" msprop:OraDbType="126" type="xs:string" minOccurs="0" /> 
       <xs:element name="NO_OF_PEOPLE" msprop:OraDbType="111" type="xs:short" minOccurs="0" /> 
       <xs:element name="ROOM_COST" msprop:OraDbType="108" type="xs:double" minOccurs="0" /> 
       <xs:element name="ROOM_TYPE" msprop:OraDbType="126" type="xs:string" minOccurs="0" /> 
       <xs:element name="ROOM_DESC" msprop:OraDbType="126" type="xs:string" minOccurs="0" /> 
       <xs:element name="BOOKING_ID" msprop:OraDbType="111" type="xs:short" minOccurs="0" /> 
       <xs:element name="DATE_FROM" msprop:OraDbType="106" type="xs:dateTime" minOccurs="0" /> 
       <xs:element name="DATE_TO" msprop:OraDbType="106" type="xs:dateTime" minOccurs="0" /> 
       <xs:element name="HOTEL_AREA" msprop:OraDbType="126" type="xs:string" minOccurs="0" /> 
       <xs:element name="HOTEL_NAME" msprop:OraDbType="126" type="xs:string" minOccurs="0" /> 
       <xs:element name="RESTAURANT" msprop:OraDbType="126" type="xs:string" minOccurs="0" /> 
       </xs:sequence> 
      </xs:complexType> 
      </xs:element> 
     </xs:choice> 
     </xs:complexType> 
    </xs:element> 
    </xs:schema> 
    <Table> 
    <ROOM_ID>101</ROOM_ID> 
    <SMOKING>N</SMOKING> 
    <NO_OF_PEOPLE>1</NO_OF_PEOPLE> 
    <ROOM_COST>29.990000000000002</ROOM_COST> 
    <ROOM_TYPE>SINGLE ROOM</ROOM_TYPE> 
    <ROOM_DESC>ONE BED, MIRROR, DESK, COUCH</ROOM_DESC> 
    <BOOKING_ID>300</BOOKING_ID> 
    <DATE_FROM>2008-01-02T00:00:00+00:00</DATE_FROM> 
    <DATE_TO>2008-01-04T00:00:00+00:00</DATE_TO> 
    <HOTEL_AREA>WOLVERHAMPTON</HOTEL_AREA> 
    <HOTEL_NAME>LEVELS</HOTEL_NAME> 
    <RESTAURANT>N</RESTAURANT> 
    </Table> 
    <Table> 
    <ROOM_ID>105</ROOM_ID> 
    <SMOKING>Y</SMOKING> 
    <NO_OF_PEOPLE>4</NO_OF_PEOPLE> 
    <ROOM_COST>59.99</ROOM_COST> 
    <ROOM_TYPE>EN-SUITE</ROOM_TYPE> 
    <ROOM_DESC>KINGSIZE BED, MIRROR, GARDEN</ROOM_DESC> 
    <BOOKING_ID>301</BOOKING_ID> 
    <DATE_FROM>2009-01-02T00:00:00+00:00</DATE_FROM> 
    <DATE_TO>2009-01-15T00:00:00+00:00</DATE_TO> 
    <HOTEL_AREA>STAFFORD</HOTEL_AREA> 
    <HOTEL_NAME>HILTON</HOTEL_NAME> 
    <RESTAURANT>Y</RESTAURANT> 
    </Table> 
    <Table> 
    <ROOM_ID>111</ROOM_ID> 
    <SMOKING>N</SMOKING> 
    <NO_OF_PEOPLE>10</NO_OF_PEOPLE> 
    <ROOM_COST>49.99</ROOM_COST> 
    <ROOM_TYPE>HOSTEL ROOM</ROOM_TYPE> 
    <ROOM_DESC>DOUBLE-DECKER BED, DRAWER</ROOM_DESC> 
    <BOOKING_ID>302</BOOKING_ID> 
    <DATE_FROM>2009-03-02T00:00:00+00:00</DATE_FROM> 
    <DATE_TO>2009-06-15T00:00:00+01:00</DATE_TO> 
    <HOTEL_AREA>WOLVERHAMPTON</HOTEL_AREA> 
    <HOTEL_NAME>LEVELS</HOTEL_NAME> 
    <RESTAURANT>N</RESTAURANT> 
    </Table> 
    <Table> 
    <ROOM_ID>113</ROOM_ID> 
    <SMOKING>N</SMOKING> 
    <NO_OF_PEOPLE>1</NO_OF_PEOPLE> 
    <ROOM_COST>29.990000000000002</ROOM_COST> 
    <ROOM_TYPE>SINGLE ROOM</ROOM_TYPE> 
    <ROOM_DESC>ONE BED, MIRROR, DESK, COUCH</ROOM_DESC> 
    <BOOKING_ID>303</BOOKING_ID> 
    <DATE_FROM>2009-09-02T00:00:00+01:00</DATE_FROM> 
    <DATE_TO>2009-09-05T00:00:00+01:00</DATE_TO> 
    <HOTEL_AREA>BASWICH</HOTEL_AREA> 
    <HOTEL_NAME>DAYSTAR</HOTEL_NAME> 
    <RESTAURANT>Y</RESTAURANT> 
    </Table> 
    <Table> 
    <ROOM_ID>103</ROOM_ID> 
    <SMOKING>Y</SMOKING> 
    <NO_OF_PEOPLE>2</NO_OF_PEOPLE> 
    <ROOM_COST>39.99</ROOM_COST> 
    <ROOM_TYPE>DOUBLE ROOM</ROOM_TYPE> 
    <ROOM_DESC>DOUBLE BED, MIRROR, SEETEE</ROOM_DESC> 
    <BOOKING_ID>304</BOOKING_ID> 
    <DATE_FROM>2009-08-21T00:00:00+01:00</DATE_FROM> 
    <DATE_TO>2009-08-25T00:00:00+01:00</DATE_TO> 
    <HOTEL_AREA>CANNOCK</HOTEL_AREA> 
    <HOTEL_NAME>MIDAS</HOTEL_NAME> 
    <RESTAURANT>N</RESTAURANT> 
    </Table> 
    <Table> 
    <ROOM_ID>107</ROOM_ID> 
    <SMOKING>Y</SMOKING> 
    <NO_OF_PEOPLE>10</NO_OF_PEOPLE> 
    <ROOM_COST>49.99</ROOM_COST> 
    <ROOM_TYPE>HOSTEL ROOM</ROOM_TYPE> 
    <ROOM_DESC>DOUBLE-DECKER BED, DRAWER</ROOM_DESC> 
    <BOOKING_ID>305</BOOKING_ID> 
    <DATE_FROM>2009-08-25T00:00:00+01:00</DATE_FROM> 
    <DATE_TO>2009-08-31T00:00:00+01:00</DATE_TO> 
    <HOTEL_AREA>STOKE</HOTEL_AREA> 
    <HOTEL_NAME>MIVI</HOTEL_NAME> 
    <RESTAURANT>Y</RESTAURANT> 
    </Table> 
    <Table> 
    <ROOM_ID>141</ROOM_ID> 
    <SMOKING>N</SMOKING> 
    <NO_OF_PEOPLE>2</NO_OF_PEOPLE> 
    <ROOM_COST>39.99</ROOM_COST> 
    <ROOM_TYPE>DOUBLE ROOM</ROOM_TYPE> 
    <ROOM_DESC>DOUBLE BED, MIRROR, SEETEE</ROOM_DESC> 
    <HOTEL_AREA>BASWICH</HOTEL_AREA> 
    <HOTEL_NAME>DAYSTAR</HOTEL_NAME> 
    <RESTAURANT>Y</RESTAURANT> 
    </Table> 
    <Table> 
    <ROOM_ID>109</ROOM_ID> 
    <SMOKING>Y</SMOKING> 
    <NO_OF_PEOPLE>1</NO_OF_PEOPLE> 
    <ROOM_COST>19.990000000000002</ROOM_COST> 
    <ROOM_TYPE>BED-SIT</ROOM_TYPE> 
    <ROOM_DESC>ONE BED, DESK</ROOM_DESC> 
    <HOTEL_AREA>CANNOCK</HOTEL_AREA> 
    <HOTEL_NAME>MIDAS</HOTEL_NAME> 
    <RESTAURANT>N</RESTAURANT> 
    </Table> 
</NewDataSet> 

XDocument xmlDoc = XDocument.Load(Server.MapPath("xml/data1.xml")); 

     var hotels = from hotel in xmlDoc.Descendants("Table") 
        where Double.Parse(pplTextBox.Text) <= Double.Parse(hotel.Element("NO_OF_PEOPLE").Value) 
        //&& DateTime.Parse(DateTextFrom.Text)>DateTime.Parse(hotel.Element("DATE_TO").Value) 
        select new 
        { 
         RoomCost = hotel.Element("ROOM_COST").Value, 
         RoomType = hotel.Element("ROOM_TYPE").Value, 
         Hotel = hotel.Element("HOTEL_NAME").Value, 
         NoOfGuest = hotel.Element("NO_OF_PEOPLE").Value, 
         Smoking = hotel.Element("SMOKING").Value, 
         Restaurant = hotel.Element("RESTAURANT").Value, 
         Location = hotel.Element("HOTEL_AREA").Value, 
         AvailableDate = hotel.Element("DATE_TO").Value 

        }; 


     GridView1.DataSource = hotels.ToList(); 

     GridView1.DataBind(); 

在此先感謝。

+0

http://stackoverflow.com/questions/1316373/displaying-results-of-a-linq-to-xml-query-in-gridview – CoderDennis 2009-08-26 21:08:16

+0

這個似乎更好地說明問題是什麼,儘管用戶應該學會只編輯問題而不是重新發布:) – 2009-09-10 14:07:56

回答

0

這會給你強類型的值允許格式化對象:

select new { 
    RoomCost = (decimal)hotel.Element("ROOM_COST"), 
    RoomType = (string)hotel.Element("ROOM_TYPE"), 
    Hotel = (string)hotel.Element("HOTEL_NAME"), 
    NoOfGuest = (int)hotel.Element("NO_OF_PEOPLE"), 
    Smoking = (hotel.Element("SMOKING").Value == "Y"), 
    Restaurant = (hotel.Element("RESTAURANT").Value == "Y"), 
    Location = (string)hotel.Element("HOTEL_AREA"), 
    AvailableDate = (DateTime)hotel.Element("DATE_TO") 
}; 

此外,我會建議解析和第一驗證用戶輸入,然後改變你的查詢到這一點:

var query = from hotel in data.Descendants("Table") 
      where numberOfPeople <= (double)hotel.Element("NO_OF_PEOPLE") 
      && dateNeeded > (DateTime)hotel.Element("DATE_FROM") 
      && dateNeeded < (DateTime)hotel.Element("DATE_TO") 

創建一個名爲「numberOfPeople」的變量並將輸入解析爲與「dateNeeded」相同的輸入,警告用戶輸入的內容無法解析......不想將它們與一些奇怪的Linq一起呈現給Xml錯誤:)