2012-03-29 87 views
1

這裏是我試圖追加一個節點在現有的XML代碼中,XML添加新元素內現有的XML

XmlDocument doc = new XmlDocument(); 
XmlNode root = doc.DocumentElement; 
XmlElement elem = doc.CreateElement("price"); 
doc.LoadXml("<Hotel><OptionNo>1</OptionNo><StayDateRange Start=\"15/04/2012\" End=\"17/04/2012\"/><Property Id=\"31461\" CityCode=\"666\" CityName=\"MUMBAI\" CountryCode=\"246\" CountryName=\"INDIA\" StateCode=\"7332\" StateName=\"Maharashtra\" StarRating=\"1\" Chain=\"\" Status=\"Available\" HotelName=\"Airlines International Hotel\" Preferred=\"Yes\" BuiltYear=\"\" RenovationYear=\"\" NoOfFloors=\"\" Location=\"YOGA INSTITUTE RD,PRABHAT COLONY,SANTACRUZ(E),MUMB Mumbai\" MinimumRate=\"4733.43\" Currency=\"INR\"><Address><Address1>YOGA INSTITUTE RD,PRABHAT COLONY,SANTACRUZ(E),MUMB Mumbai</Address1><Address2/><CityCode>666</CityCode><CountryCode>246</CountryCode><PostalCode>400055</PostalCode><CityName>MUMBAI</CityName></Address><Description Name=\"Comprising of 27 rooms in total, this small budget hotel is clean and comfortable, making it the ideal choice for both business and leisure travellers. Fully air-conditioned, it features a 24-hour reception, currency exchange facilities, lift access and an on-site restaurant. Room and laundry services are also available.\" Url=\"\"/><GeoCode Code=\"\" longitude=\"\" latitude=\"\"/><Images><HotelFrontImage Url=\"\"/><Room Url=\"\"/></Images><OtherHotelImages/><Features/><Transportation/><Attractions/></Property><Rooms><Room NumberOfRooms=\"1\" RoomId=\"4511\" Description=\"Double Room\" Twin=\"No\" SupplierCode=\"HP\" SupplierId=\"4\" PropertyId=\"31461\" ExtraBed=\"0\" RateBasis=\"0\" Type=\"Double\"><Rate Id=\"4511\" Currency=\"INR\" Gross=\"9466.86\" Net=\"9466.86\" Tax=\"0\" ExtraGuestCharge=\"0.00\" AdultCount=\"2\" AdultRate=\"\" ChildCount=\"0\" ChildRate=\"\" Description=\"Bed and Breakfast\" Status=\"Available\" AllocationDetails=\"OC-84296147\"><Nights><Night Date=\"1\" Gross=\"4733.43\" Net=\"4733.43\" Tax=\"0.00\" AdultCount=\"2\" AdultRate=\"0\" ChildCount=\"0\" ChildRate=\"0\" ExtraGuestCharge=\"0\" BookedDate=\"15/04/2012\" FreeStay=\"\" RateSupplierId=\"1\" Status=\"Available\"/><Night Date=\"2\" Gross=\"4733.43\" Net=\"4733.43\" Tax=\"0.00\" AdultCount=\"2\" AdultRate=\"0\" ChildCount=\"0\" ChildRate=\"0\" ExtraGuestCharge=\"0\" BookedDate=\"16/04/2012\" FreeStay=\"\" RateSupplierId=\"1\" Status=\"Available\"/></Nights><MinStay/><DateApplyMinStay/><CancellationRules>Please see the cancellation rules</CancellationRules></Rate><Ages/><Amenities/></Room></Rooms></Hotel>"); 
root = doc.DocumentElement; 
elem = doc.CreateElement("OtherHotelImage"); 
elem.InnerText = "URL"; 
root.AppendChild(elem); 
doc.Save(Console.Out); 

我嘗試添加指定的XML,這是<OtherHotelImages>元件的內部內新節點。但它是在根元素<Hotel>內部創建的。請建議,這是緊急的。

+1

請不要在帖子中使用大寫字母 - 它被認爲是大喊大叫。 – JTeagle 2012-03-29 07:24:25

回答

0

你正在調用root.AppendChild()...所以是的,它進入根。您需要走XML樹,直到找到合適的元素 - 然後然後將新元素添加到

+2

你能建議我該怎麼做?任何示例或網址? – Sujit 2012-03-29 07:30:27

+0

看看GetElementsByTagName():http://msdn.microsoft.com/en-us/library/dc0c9ekk.aspx - 這可以得到一個OtherHotelImages標籤的列表(如果這是唯一發生的)。 – JTeagle 2012-03-29 07:36:17