如何在 C# 中直接调用嵌套的 xml 元素
How to call for a nested xml element directly in c#
这是我的xml...
<CruiseProduct>
<ID>4091</ID>
<Name>MS ROYAL RUBY NILE CRUISE</Name>
<Description>
<p><br>In house music panel.<br>Safe deposit box.<br>All modern and high quality installation and amenities.<br><br><br><b>All inclusive formula</b><br>Breakfast open buffet<br>Lunch open buffet<br>dinner open buffet<br>From 11.00 AM till 23.00 HRS<br>Water (Small Bottle)<br>Soft drinks<br>Local beer (Served by glass)<br>Local wine (Only during lunch and dinner)</p><p><br><b><b>The Program includes:</b><br></b>♦ 07 nights’ accommodation on board of MS Royal Ruby or Similar based on Full Board<br>♦ Return airport transfers in Luxor using Shuttle services<br>♦ Guaranteed upgrade to Main / Middle deck cabins.<br>♦ Complimentary Luxor city tour<b><br><br><b>The Program excludes:</b><br></b>♦ Entry visa to Egypt<br>♦ Tips and personal expenses<br>♦ Any other items did not mentioned above</p><p><b><br></b></p><p><b>Attraction - Combo Light Package</b><br>Visit to the east Bank, Karnak and Luxor temples, Visit the Temple shared by two gods Sobek and Haeroris in Kom Ombo afternoon, Morning visit to High dam, Phila temple<br><b><br>Attraction - Combo Full Package Excursions</b><br>Visit to the East Bank, Karnak and Luxor temples, Visit the Temple shared by two gods Sobek and Haeroris in Kom Ombo afternoon, Visit Edfu Temple, Morning visit to High dam, Philae temple and unfinished obelisk, on return visits West bank, Hatsheput, Valley of the Kings, Memmon colossi Esna Temple<br><br><b>Attraction - Cruise Signature Program</b><br>Visit Dier-al-Madina + Habu Temple + Valley of the Nobles, Dendra Temple by bus + Temple of Hathor-Cript, Edfu OR Kom Ombo Temple (up to the customer) Kalabsha trip + Botanical Gardens + 01 hour felucca </p>
</Description>
<Ref>CMBRUB</Ref>
<Location>Luxor</Location>
<Images>
<Image>
<Name>MS Royal Ruby</Name>
<Description/>
<URL>
http://banks.digital-trip.co.uk/assets/images/packages/c228fa2c-18b1-4c93- 9edc-aa3409d07b7b.jpg
</URL>
</Image>
这是我的代码....
var product = from a in cruiseDoc.Descendants("CruiseProduct")
select new CProducts
{
cruise_Id = a.Element("ID").Value,
cruise_Name = a.Element("Name").Value,
cruise_description= a.Element("Description").Value,
// cruise_Imagerurl = a.Descendants("")
};
var newProduct = product.ToArray();
ViewBag.Lengths = newProduct;
我想立即调用 imager URL 元素并将其值赋给 cruise_Imageurl 变量。
希望你的帮助
现在我们知道您只需要 第一个 图像元素,这很简单:
cruise_Imagerurl = a.Element("Images").Element("Image").Element("URL").Value
这假设总是 是 至少一张图片,请注意。如果不是这样,您可能需要:
cruise_Imagerurl = (string) a.Elements("Images")
.Elements("Image")
.Elements("URL")
.FirstOrDefault()
这是我的xml...
<CruiseProduct>
<ID>4091</ID>
<Name>MS ROYAL RUBY NILE CRUISE</Name>
<Description>
<p><br>In house music panel.<br>Safe deposit box.<br>All modern and high quality installation and amenities.<br><br><br><b>All inclusive formula</b><br>Breakfast open buffet<br>Lunch open buffet<br>dinner open buffet<br>From 11.00 AM till 23.00 HRS<br>Water (Small Bottle)<br>Soft drinks<br>Local beer (Served by glass)<br>Local wine (Only during lunch and dinner)</p><p><br><b><b>The Program includes:</b><br></b>♦ 07 nights’ accommodation on board of MS Royal Ruby or Similar based on Full Board<br>♦ Return airport transfers in Luxor using Shuttle services<br>♦ Guaranteed upgrade to Main / Middle deck cabins.<br>♦ Complimentary Luxor city tour<b><br><br><b>The Program excludes:</b><br></b>♦ Entry visa to Egypt<br>♦ Tips and personal expenses<br>♦ Any other items did not mentioned above</p><p><b><br></b></p><p><b>Attraction - Combo Light Package</b><br>Visit to the east Bank, Karnak and Luxor temples, Visit the Temple shared by two gods Sobek and Haeroris in Kom Ombo afternoon, Morning visit to High dam, Phila temple<br><b><br>Attraction - Combo Full Package Excursions</b><br>Visit to the East Bank, Karnak and Luxor temples, Visit the Temple shared by two gods Sobek and Haeroris in Kom Ombo afternoon, Visit Edfu Temple, Morning visit to High dam, Philae temple and unfinished obelisk, on return visits West bank, Hatsheput, Valley of the Kings, Memmon colossi Esna Temple<br><br><b>Attraction - Cruise Signature Program</b><br>Visit Dier-al-Madina + Habu Temple + Valley of the Nobles, Dendra Temple by bus + Temple of Hathor-Cript, Edfu OR Kom Ombo Temple (up to the customer) Kalabsha trip + Botanical Gardens + 01 hour felucca </p>
</Description>
<Ref>CMBRUB</Ref>
<Location>Luxor</Location>
<Images>
<Image>
<Name>MS Royal Ruby</Name>
<Description/>
<URL>
http://banks.digital-trip.co.uk/assets/images/packages/c228fa2c-18b1-4c93- 9edc-aa3409d07b7b.jpg
</URL>
</Image>
这是我的代码....
var product = from a in cruiseDoc.Descendants("CruiseProduct")
select new CProducts
{
cruise_Id = a.Element("ID").Value,
cruise_Name = a.Element("Name").Value,
cruise_description= a.Element("Description").Value,
// cruise_Imagerurl = a.Descendants("")
};
var newProduct = product.ToArray();
ViewBag.Lengths = newProduct;
我想立即调用 imager URL 元素并将其值赋给 cruise_Imageurl 变量。 希望你的帮助
现在我们知道您只需要 第一个 图像元素,这很简单:
cruise_Imagerurl = a.Element("Images").Element("Image").Element("URL").Value
这假设总是 是 至少一张图片,请注意。如果不是这样,您可能需要:
cruise_Imagerurl = (string) a.Elements("Images")
.Elements("Image")
.Elements("URL")
.FirstOrDefault()