开发者

How can i select specific childnodes with LINQ?

开发者 https://www.devze.com 2023-01-19 08:49 出处:网络
I have a node and this node contains 5 childnodes. three of them is RatePlan. How can i select those RatePlan childnodes with LINQ?

I have a node and this node contains 5 childnodes. three of them is RatePlan. How can i select those RatePlan childnodes with LINQ?

Lets clarify something :

my xml is like this :

<hotels>
<hotel id="1" name="hotel 1">
    <telephone>123456789</telephone>
    <fax>123</fax>
    <address>hotels address</address>
    <hotelRatePlan>10</hotelRatePlan>
    <hotelRatePlan>11</hotelRatePlan>
    <hotelRatePlan>12</hotelRatePlan>
  </hotel>
  <hotel id="2" name="hotel 2">
    <telephone>123456789</teleph开发者_Python百科one>
    <fax>123</fax>
    <address>hotels address</address>
    <hotelRatePlan>100</hotelRatePlan>
    <hotelRatePlan>110</hotelRatePlan>
    <hotelRatePlan>120</hotelRatePlan>
  </hotel>
  <hotel id="3" name="hotel 3">
    <telephone>123456789</telephone>
    <fax>123</fax>
    <address>hotels address</address>
    <hotelRatePlan>10</hotelRatePlan>
    <hotelRatePlan>11</hotelRatePlan>
    <hotelRatePlan>12</hotelRatePlan>
  </hotel>
</hotels>

I am using XMLDocument to read XML file. After i read it i make a selection with SelectNodes. When i get first hotel information i want to select specific childnodes (hotelRatePlan). How can i do that?


Your question isn't particularly clear, but you might just want:

var ratePlans = node.Elements("RatePlan");

That's assuming you're actually using LINQ to XML rather than XmlNode, XmlDocument etc. If you are using the "old" DOM API, you could use:

var ratePlans = node.ChildNodes
                    .OfType<XmlElement>()
                    .Where(e => e.LocalName == "RatePlan");

... but I'd moving to LINQ to XML if you can. It's simply a much nicer XML API.


If you are sure that you will only have three rate plans per hotel, then you can load a hotel into an object of type Hotel like so:

XDocument data = XDocument.Load(yourXMLFileNameHere);
//if you have a namespace defined:
XNamespace ns = data.Root.Name.Namespace;

List<Hotels> hotels = (from item in data.Descendants(ns + "hotel")
                       select new Hotel
                       {
                         Id=Convert.ToInt32(item.Attribute("id").Value),
                         Name=item.Attribute("name").Value,
                         Telephone=item.Element(ns+"telephone").Value,
                         Fax=item.Element(ns+"fax").Value, 
                         Address=item.Element(ns+"address").Value,
                         RatePlan1=item.Element(ns+"hotelRatePlan1").Value,
                         RatePlan2=item.Element(ns+"hotelRatePlan2").Value,
                         RatePlan3=item.Element(ns+"hotelRatePlan3").Value
                        }).ToList<Hotels>();

And then you reference your first rate plan in the following way:

string ratePlan1=hotels[0].RatePlan1;

If the number of your rate plans will vary, you can merge them together into a string like so:

<hotelRatePlans>10 20 30</hotelRatePlans>

Then you change the way you extract your rate plans, and when you need the actual plans, you use the String.Split method to get the array of individual plans.


I think you mean:

var ratePlans = node.ChildNodes.OfType<RatePlan>();

0

精彩评论

暂无评论...
验证码 换一张
取 消