XML在现有XML中添加新元素(Adding new XML elements inside existing XML)

编程入门 行业动态 更新时间:2024-10-19 04:32:23
XML在现有XML中添加新元素(Adding new XML elements inside existing 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>的根元素内创建的。 请建议,这很紧急。

Here is the code I have tried to append a node in existing 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);

I tried to add new node inside the specified xml, which is inside of <OtherHotelImages> element. but it is created inside of root element which is <Hotel>.

最满意答案

你正在调用root.AppendChild()...所以是的,它进入根目录。 您需要遍历XML树,直到找到正确的元素 - 然后将新元素添加到该树。

You are calling root.AppendChild()... so yes, it goes in at the root. You need to walk the XML tree until you find the right element - and then add the new element to that.

更多推荐

本文发布于:2023-08-02 20:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1379601.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:新元素   XML   Adding   existing   elements

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!