我XML
在string __MessageIn
变量中有以下格式化的字符串:
string _MessageIn=
<?Label TSRAY|RESERVATION|317859|SUCCESS?>
<Reservation xmlns="reservation.fidelio.2.0" mfShareAction="NA" mfReservationAction="EDIT">
<HotelReference>
<hotelCode>TSRAY</hotelCode>
</HotelReference>
<confirmationID>Y6Z7TFJDK</confirmationID>
<reservationID>347557</reservationID>
<reservationOriginatorCode>JA</reservationOriginatorCode>
<originalBookingDate>2010-08-16T22:53:23.000</originalBookingDate>
<StayDateRange timeUnitType="DAY">
<startTime>2010-08-19T00:00:00.000</startTime>
<numberOfTimeUnits>3</numberOfTimeUnits>
</StayDateRange>
<GuestCounts>
<GuestCount>
<ageQualifyingCode>ADULT</ageQualifyingCode>
<mfCount>2</mfCount>
</GuestCount>
<GuestCount>
<ageQualifyingCode>CHILD</ageQualifyingCode>
<mfCount>0</mfCount>
</GuestCount>
</GuestCounts>
...................
..................
</Reservation>
我试图从中获取hotelcode的值,即TSRAY,<hotelCode>TSRAY</hotelCode>
但是代码始终返回NULL
In HotelReference
。
我尝试了下面的代码行:
// query the XML document
XDocument doc = XDocument.Parse(_MessageIn);
var HotelReference = doc.Descendants("HotelReference").Select(x => new { HotelCode = x.Element("hotelCode").Value}).FirstOrDefault();
如何从上述XML
格式中获取所需的值string variable
?
提前致谢!
发生这种情况是因为名称空间是xml的一部分。
有两种方法可以解决此问题,或者删除名称空间“ reservation.fidelio.2.0”,然后您的代码将可以正常工作。但这可能不可行。
因此,在执行搜索时,我们必须将名称空间与节点名一起传递。尝试以下方法,这将解决您的问题。希望这可以帮助。
string ns = "reservation.fidelio.2.0";
string node = "HotelReference";
string elem = "hotelCode";
XName xn = XName.Get(node, ns);
XName xe = XName.Get(elem, ns);
var HotelReference = doc.Root.Descendants(xn).Select(x => new { HotelCode = x.Element(xe).Value }).FirstOrDefault();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句