使用linq读取xml节点及其属性 [英] Read xml node with its attribute using linq
问题描述
我有一个如下所示的xml字符串
i have a xml string like this below
<HotelSummary order="4" ubsScore="59127"><hotelId>197563</hotelId><name>Metro Apartments on Darling Harbour - Sydney</name><address1>132 - 136 Sussex Street</address1><city>Sydney</city><stateProvinceCode>NW</stateProvinceCode><postalCode>2000</postalCode><countryCode>AU</countryCode><airportCode>SYD</airportCode><supplierType>E</supplierType><propertyCategory>4</propertyCategory><hotelRating>3.5</hotelRating><confidenceRating>52</confidenceRating><amenityMask>0</amenityMask><tripAdvisorRating>3.5</tripAdvisorRating><tripAdvisorReviewCount>145</tripAdvisorReviewCount><tripAdvisorRatingUrl>http://www.tripadvisor.com/img/cdsi/img2/ratings/traveler/3.5-12345-4.gif</tripAdvisorRatingUrl><locationDescription>Near Sydney Aquarium</locationDescription><shortDescription><p><b>Property Location</b> <br />With a stay at Metro Apartments on Darling Harbour - Sydney, you'll be centrally located in Sydney, steps from Sydney Aquarium and Wildlife Sydney. This</shortDescription><highRate>344</highRate><lowRate>244</lowRate><rateCurrencyCode>AUD</rateCurrencyCode><latitude>-33.86874</latitude><longitude>151.20361</longitude><proximityDistance>0.1874232</proximityDistance><proximityUnit>MI</proximityUnit><hotelInDestination>true</hotelInDestination><thumbNailUrl>/hotels/1000000/860000/852700/852680/852680_37_t.jpg</thumbNailUrl><deepLink>http://travel.ian.com/index.jsp?pageName=hotAvail&cid=55505&hotelID=197563&mode=2&numberOfRooms=1&room-0-adult-total=2&room-0-child-total=2&room-0-child-0-age=5&room-0-child-1-age=7&arrivalMonth=11&arrivalDay=13&departureMonth=11&departureDay=15&showInfo=true&locale=en_US&currencyCode=AUD</deepLink><RoomRateDetailsList><RoomRateDetails><roomTypeCode>24056</roomTypeCode><rateCode>1444320</rateCode><maxRoomOccupancy>4</maxRoomOccupancy><quotedRoomOccupancy>2</quotedRoomOccupancy><minGuestAge>0</minGuestAge><roomDescription>One Bedroom Apartment</roomDescription><propertyAvailable>true</propertyAvailable><propertyRestricted>false</propertyRestricted><expediaPropertyId>852680</expediaPropertyId><RateInfos size="1"><RateInfo priceBreakdown="true" promo="false" rateChange="true"><RoomGroup><Room><numberOfAdults>2</numberOfAdults><numberOfChildren>2</numberOfChildren><childAges>5</childAges><childAges>7</childAges></Room></RoomGroup><ChargeableRateInfo averageBaseRate="294.0" averageRate="294.0" commissionableUsdTotal="538.66" currencyCode="AUD" maxNightlyRate="344.0" nightlyRateTotal="588.0" surchargeTotal="144.0" total="732.0"><NightlyRatesPerRoom size="2"><NightlyRate baseRate="244.0" rate="244.0" promo="false"/><NightlyRate baseRate="344.0" rate="344.0" promo="false"/></NightlyRatesPerRoom><Surcharges size="1"><Surcharge type="ExtraPersonFee" amount="144.0"/></Surcharges></ChargeableRateInfo><nonRefundable>false</nonRefundable><rateType>MerchantStandard</rateType><currentAllotment>2</currentAllotment></RateInfo></RateInfos><ValueAdds size="1"><ValueAdd id="2048"><description>Free Wireless Internet</description></ValueAdd></ValueAdds></RoomRateDetails></RoomRateDetailsList></HotelSummary>
< br $>
i希望找到一个属于purticular元素的属性。
在我的xml中我想找到属性'surchargeTotal'里面的内容
'CableableRateInfo节点'。
有时我我的xml'contistantTotal'属性不会出现所以这就是为什么
i需要检查它是否可用?
im检查天气'附加费'是否可用或不是吗?
如果可用,那么在相关变量中存储true否则为假
我的代码是:
i want to find an attribute with is inside the purticular element.
in my xml i want to find attribute 'surchargeTotal' which is inside
'ChargeableRateInfo node'.
sometimes in my xml 'surchargeTotal' attribute is not coming so that's why
i need to check wheather its availble or not?
i m getting the issue in checking wheather 'surchargeTotal' is available or not?
if available then store true in relevent variable otherwise false
my code is :
XDocument xdoc = XDocument.Load(@"C:/1.xml");
var linq = from item in xdoc.Descendants("HotelList").Elements("HotelSummary")
select new
{
hotelId = item.Element("hotelId").Value,
name = item.Element("name").Value,
desc = item.Element("shortDescription").Value,
img = "http://images.travelnow.com" + item.Element("thumbNailUrl").Value.ToString().Replace("_t.jpg", "_s.jpg"),
surchargeTotal = item.Element("RoomRateDetailsList").Element("RoomRateDetails").Element("RateInfos").Element("RateInfo").Elements("ChargeableRateInfo").Attributes("averageRate").ToString() //!= null ? "hi" : "hello"
};
foreach (var node in linq)
{
lblMsgs.Text += "Hotel ID : " + node.hotelId + ", Hotel Name : " + node.name + ", Desc : " + node.desc + ", Hotel Img : " + node.img + ", total : " + node.surchargeTotal + "<br/><br/><br/>";
}
i想存储这样的价值
i want to store the value like this
(bool)item.Element("RoomRateDetailsList").Element("RoomRateDetails").Element("RateInfos").Element("RateInfo").Elements("ChargeableRateInfo").Attributes("averageRate") != null ? true : false
请帮助我....
please help me....
推荐答案
我自己解决了这个问题
刚放好
i solved this by myself
just put
averageRate= item.Elements("RoomRateDetailsList").Elements("RoomRateDetails").Elements("RateInfos").Elements("RateInfo").Elements("ChargeableRateInfo").Attributes("averageRate").Take(1).Any()
如果averageRate将返回true属性可用,否则为假。
如果你想检索属性值,那就把
this will return true if averageRate attribute is available otherwise false.
and if you want to retrieve the attribute value then just put
total = item.Descendants("ChargeableRateInfo").Select(ele => (string)ele.Attribute("total").Value).FirstOrDefault()
享受coading ..........
enjoy coading..........
这篇关于使用linq读取xml节点及其属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!