I have an XML document similar to this:
<document>
<post>
<author>Bill Smith</author>
<subject>Test Article</subject>
<dates>
<uploaded>some date</uploaded>
<published>some date</published>
</dates>
<price>
<provider>Amazon</provider>
<cost>1540</cost>
</price>
<price>
<provider>WH Smith</provider>
<cost>2640</cost>
</price>
</post>
<post>
<author>Bill Smith</author>
<subject>Test Article</subject>
<dates>
<uploaded>some date</uploaded>
<published>some date</published>
</dates>
<price>
<provider>Amazon</provider>
<cost>1540</cost>
</price>
<price>
<provider>WH Smith</provider>
<cost>2640</cost>
</price>
</post>
</document>
I'm using XDocument w/ .NET 4.5. I know there are other methods I could use to sort this.
I have it working OK to pull each post and put it into a Post model. However, I would like to sort the price elements and pick out the lowest price (and also the provider) so I can insert it into my EF database.
Any help would be much appreciated, I'm totally stuck on where to even start with this.
You can give this a try:
XDocument doc = XDocument.Load(#"Data.xml");
var elements = doc.Root
.Elements("post")
.Select(post => new
{
Author = post.Element("author").Value,
Subject = post.Element("subject").Value,
Uploaded = Convert.ToDateTime(post.Element("dates").Element("uploaded").Value),
Published = Convert.ToDateTime(post.Element("dates").Element("published").Value),
Price = new
{
P = post
.Elements("price")
.OrderByDescending(price => Convert.ToDecimal(price.Element("cost").Value))
.Select(o => new
{
Provider = o.Element("provider").Value,
Cost = Convert.ToDecimal(o.Element("cost").Value)
})
.First()
}
});
var p = elements.First();
Based on code you provided, see, if this works:
var xmlDocumentElement = xDocument.Load("xmlData.xml").Root;
var posts = xmlDocumentElement.Elements("post").Select(post => new
{
Author = post.Element("author").Value.ToString(),
Subject = post.Element("subject").Value.ToString(),
AvailableDate = DateTime.Parse(post.Descendants("dates").FirstOrDefault().Value),
Price = GetPriceFromPriceXElement(post.Elements("price").Aggregate((prev, next) =>
Decimal.Parse(prev.Element("cost").Value.ToString()) <= Decimal.Parse(next.Element("cost").Value.ToString()) ? prev : next ))
}
);
public Price GetPriceFromPriceXElement(XElement price)
{
return new Price
{
Provider = price.Element("provider").Value.ToString(),
Cost = price.Element("cost").Value.ToString()
};
}
Try this. It should work. Once you've data, you can massage it. Please take care of NULL etc. This is just basic idea around your requirement.
XDocument doc = XDocument.Load(#"XMLFile1.xml");
var posts = doc.Root.Elements("post")
.Select(p =>
new
{
Author = p.Element("author").Value,
Price = p.Elements("price").OrderBy(a => decimal.Parse(a.Element("cost").Value)).First()
});
foreach(var a in posts)
{
Console.WriteLine(a.Author + " " + a.Price);
}
Here is a pretty straightforward attempt (and actually very similar to Ivan G's answer):
var posts = from post in document.Root.Elements("post")
select new
{
Author = post.Element("author").Value,
Subject = post.Element("subject").Value,
Uploaded = DateTime.Parse(post.Element("dates").Element("uploaded").Value),
Published = DateTime.Parse(post.Element("dates").Element("published").Value),
Price = (from price in post.Elements("price")
let cost = decimal.Parse(price.Element("cost").Value)
orderby cost
select new
{
Provider = price.Element("provider").Value,
Cost = cost
}).First()
};
It works fine for the sample data you gave us (except the dates "some date").
If the data might be incomplete or wrong (i.e. missing/misspelled nodes or wrong datatypes) I would suggest to just take each XElement instead of the values: Author = post.Element("author") etc. Maybe you want to allow posts without an uploaded or published date. This way you can inspect and/or cast/convert data later and maybe fix problems properly.
Related
XML - Code:
<Store>
<Products>
<Product id="PROD01">
<Title>Product 1</Title>
<Description><![CDATA[Product <b>1</b> description]]></Description>
<Image>prod01.gif</Image>
<Specs>
<Spec>Good computer</Spec>
<Spec>Good display</Spec>
<Spec>Latest version</Spec>
</Specs>
<Availability>same day</Availability>
</Product>
<Product id="PROD02">
<Title>Product 2</Title>
<Description><![CDATA[Product <b>2</b> description]]></Description>
<Image>prod01.gif</Image>
<Specs>
<Spec>Good computer</Spec>
<Spec>Soon available</Spec>
</Specs>
<Availability>next day</Availability>
</Product>
</Products>
</Store>
C# - Code:
public List<DetailList> GetDetails()
{
DetailList d = new DetailList();
List<DetailList> DetailLists =
(from product in xdocList.Descendants("Product")
join detail in xdocDetail.Descendants("Product")
on (string)product.Attribute("id") equals (string)detail.Attribute("id")
into outerProducts
from outerProduct in outerProducts
select new DetailList
{
Detail1 = (string)product.Attribute("id"),
Detail2 = (string)product.Element("Title"),
Detail3 = (string)product.Element("Description"),
Detail4 = (string)product.Element("Image"),
Detail5 = (string)outerProduct.Elements("Specs")
Detail6 = (string)outerProduct.Element("Availability"),
Detail7 = (string)product.Element("Price"),
}).ToList();
return DetailLists;
}
Output: Good computerGood displayLatest version
But wanted output is:
Good computer
Good display
Latest version
For output I used asp:repeater. I tried to add tags like < b r/> and much more, but cant'find my mistake, how to get Spec to three different strings, not only one string. How to achive that?
I am not sure why you are joing the nodes with self, but as per your XML it is not required. You can simply project the elements like this:-
public static List<DetailList> GetDetails(XDocument xdocList)
{
DetailList d = new DetailList();
List<DetailList> DetailLists = (from product in xdocList.Descendants("Product")
select new DetailList
{
Detail1 = ((string)product.Attribute("id")),
Detail2 = ((string)product.Element("Title")),
Detail3 = ((string)product.Element("Description")),
Detail4 = ((string)product.Element("Image")),
Detail5 = product.Element("Specs")
.Elements("Spec")
.Select(x => (string)x).ToList(),
Detail6 = ((string)product.Element("Availability")),
Detail7 = ((string)product.Element("Price")),
}).ToList();
return DetailLists;
}
Since you need all the Specs separately, you should have a collection of string and not just string. So the datatype of property Detail5 should be:-
List<string> or string[]
Friend got solution from Rahul
Detail5 = String.Join("<br/>", outerProduct.Element("Specs").Elements("Spec").Select(x => (string)x).ToList()),
Thanks #Rahul Singh
My XML response looks like this:
"<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<HotelAvailResponse xmlns="http://www.juniper.es/webservice/2007/">
<AvailabilityRS Url="http://xml.bookingengine.es"
TimeStamp="20130327T14:38:54.6916528+01:00"
IntCode="lCf65bPrG+x7VDLB0IquVNQgKloRA9+HOuhfHMj0BcE=">
<Results>
<HotelResult Code="7b0LYEzfsd0HH90sd" JPCode="JP003600" DestinationZone="39303" JPDCode="JPD000014">
<HotelInfo>
<Name>Hotel Test</Name>
<Description>A spacious, quiet, light and bright hotel, with a garden and fabulous views across the city.</Description>
<Images>
<Image>http://www.bookingengine.es/images/upload_p/hoteltest.jpeg</Image>
</Images>
<HotelCategory>1 1/2 Stars</HotelCategory>
<HotelType Type="SKI">Snow</HotelType>
<Address>c/tap</Address>
</HotelInfo>
<HotelOptions>
<HotelOption RatePlanCode="dcFZbKty1cJGKeRtgxIDGUZAprp1mua8ySl4iVIZ7NVKBF/PGk8lhZlN7Hcszjs2RwUR2Dxsrv5l0cZDORKz6frEmPdibqOyV2Jg4Dxz8/bF5gqPyQR8+z1LEu8LCVlS" Status="OK">
<Board Type="AD">Bed&Breakfast</Board>
<Prices>
<Price Type="S" Currency="USD">
<TotalFixAmounts Gross="353.65" Nett="353.65">
<Service Amount="321.5"/>
<ServiceTaxes Included="true" Amount="32.15"/>
<Commissions Included="true" Amount="0"/>
<HandlingFees Included="true" Amount="0"/>
<Discount Amount="-0"/>
</TotalFixAmounts>
</Price>
</Prices>
<HotelRooms>
<HotelRoom Units="1" Source="1" AvailRooms="12">
<Name>Double Room</Name>
<RoomCategory Type="DBL">Double Standard</RoomCategory>
</HotelRoom>
<HotelRoom Units="1" Source="2" AvailRooms="45">
<Name>Single</Name>
<RoomCategory Type="SGL">Single Standard</RoomCategory>
</HotelRoom>
</HotelRooms>
</HotelOption>
<HotelOption RatePlanCode="dcFZbKty1cJGKeRtgxIDGUZAprp1mua8ySl4iVIZ7NVKBF/PGk8lhZlN7Hcszjs2RwUR2Dxsrv5l0cZDORKz6frEmPdibqOyV2Jg4Dxz8/bmoX041DU9+3D3nHCEB/6vYKbVtJR2qaHwW9VnnWl/KA==" Status="OK">
<Board Type="AD">Bed&Breakfast</Board>
<Prices>
<Price Type="S" Currency="USD">
<TotalFixAmounts Gross="353.65" Nett="353.65">
<Service Amount="321.5"/>
<ServiceTaxes Included="true" Amount="32.15"/>
<Commissions Included="true" Amount="0"/>
<HandlingFees Included="true" Amount="0"/>
<Discount Amount="-0"/>
</TotalFixAmounts>
</Price>
</Prices>
<HotelRooms>
<HotelRoom Units="1" Source="1" AvailRooms="12">
<Name>Double Room</Name>
<RoomCategory Type="DBL">Double Standard</RoomCategory>
</HotelRoom>
<HotelRoom Units="1" Source="2" AvailRooms="11">
<Name>Double Room</Name>
<RoomCategory Type="DBL">Double Standard</RoomCategory>
</HotelRoom>
</HotelRooms>
<AdditionalElements>
<HotelOffers>
<HotelOffer>
<Name>Basic Discount 10%</Name>
</HotelOffer>
</HotelOffers>
</AdditionalElements>
</HotelOption>
</HotelOptions>
</HotelResult>
</Results>
</AvailabilityRS>
</HotelAvailResponse>
</soap:Body>
</soap:Envelope>"
And i have this Linq for that response is as follows:
XNamespace ns = "http://schemas.xmlsoap.org/soap/envelope/";
var hotels = (from hotelData in data.Descendants(ns + "Envelope").Descendants(ns + "Body").Descendants("HotelAvailResponse").Descendants("HotelAvailResult").Descendants("Results").Descendants("HotelResult")
select new Hotel
{
Code = hotelData.Attribute("Code").Value,
JpCode =
hotelData.Attributes().Any(x => x.Name == "JPCode")
? hotelData.Attribute("JPCode").Value
: "",
DestinationZone =
hotelData.Attribute("DestinationZone") != null
? hotelData.Attribute("DestinationZone").Value
: string.Empty,
JpdCode = hotelData.Attribute("JPDCode").Value,
//HotelName = Convert.ToString(hotelData.Element("Item").Value),
//Rating = Convert.ToInt32(hotelData.Element("StarRating").Value),
HotelInfo = (from hi in hotelData.Descendants("HotelInfo")
select new HotelInfo
{
Name = hi.Element("Name").Value,
Description = hi.Element("Description").Value,
Image = (from img in hi.Descendants("Images") select new Imagez { Images = img.Element("Image").Value }).ToList(),
HotelCategory = hi.Element("Name").Value,
HotelType = hi.Element("Description").Value,
Address = hi.Element("Description").Value,
}
).ToList(),
HotelOptions = (from ho in hotelData.Descendants("HotelOptions")
select new HotelOptions()
{
HotelOption = ho.Element("HotelOption").Attribute("RatePlanCode").Value,
Board = ho.Element("Board").Attribute("Type").Value,
Prices = (from pr in ho.Descendants("Prices") select new Prices() { Price = pr.Element("Price").Value,
TotalFixAmounts = (from tfa in pr.Descendants("TotalFixAmounts") select new TotalFixAmounts()
{ Service = tfa.Element("Service").Attribute("Amount").Value,
ServiceTaxes = tfa.Element("ServiceTaxes").Attribute("Included").Value,
AmountServiceTaxes = tfa.Element("ServiceTaxes").Attribute("Amount").Value,
Commissions = tfa.Element("Commissions").Attribute("Included").Value,
AmountCommissions = tfa.Element("Commissions").Attribute("Amount").Value,
HandlingFees = tfa.Element("HandlingFees").Attribute("Included").Value,
AmountHandlingFees = tfa.Element("HandlingFees").Attribute("Amount").Value,
Discount = tfa.Element("Amount").Attribute("Included").Value,
}).ToList(),
}).ToList(),
}).ToList(),
}).ToList();
return hotels;
I get no error and neither any exception but the count for hotels returned is 0.
I am a beginner at Linq.
Any help will be much appreciated, and its been 7 hours straight, I am searching for help and trying to get it done. Now I feel the dead end.
Thanks in advance.
There are a few issues with your posted code.
The first issue is that the code does not account for the namespace assigned to <HotelResult>. The namespace is http://wwww.juniper.es/webservice/2007/, and is inherited from the <HotelAvailResponse> element. You can see the namespace as an xmlns attribute:
<HotelAvailResponse xmlns="http://www.juniper.es/webservice/2007/">
This is the reason your LINQ query is not returning anything - it's looking for <HotelAvailResponse> with the XML Namespace http://schemas.xmlsoap.org/soap/envelope, and that node does not exist, so you get an empty collection. Each time there is a call to Descendants or Element, the namespace needs to be included (i.e., ns + "ElementName").
The second issue isn't readily apparent until the code acutally runs, but the following statement
HotelOptions = (from ho in hotelData.Descendants(ns + "HotelOption")
will result in <HotelOption> and <Board> appearing only once (i.e., a list of 1) rather than twice. The <price> information and <TotalFixAmounts> populates correctly - I'm not entirely sure why, but it may have to do with the nested list for <TotalFixAmounts>. This is easily fixed by changing the select to <HotelOption>, like this:
HotelOption = (from ho in hotelData.Descendants("HotelOption")
Now ho will be a collection of <HotelOption> nodes and their children, both will be processed and the nested list will be processed as well.
Next there are a couple of problems with your LINQ statement that will throw null reference exceptions (assuming the namespace issue is corrected). These are:
Board = ho.Element("Board").Attribute("Type").Value;
ho is the collection of all the <HotelOptions> nodes and their children - but <Board> is a child of <HotelOption>, which in itself is a child of <HotelOptions>. When working with XML, remember it's hierarchical in nature - .Element(elementName) will access the first element with that name that is a child (not a grand child or farther down) of the parent element. A fairly simple solution is to add <HotelOption> into the statement:
Board = ho.Element(ns + "HotelOption").Element(ns + "Board").Attribute("Type").Value;
A similar issue occurs here:
TotalFixAmounts = (from tfa in pr.Descendants("Prices") select new
pr is the collection of <Prices> nodes, but the elements being referenced in the select statement are children of <Price>, not <Prices>. Element(elementName) will get the first child node of the parent element, and <Prices> does not have any children other than <Price>.
Finally, there is no <Amount> element that is a child of <TotalFixAmounts>, so the following line will also throw a null reference exception:
Discount = tfa.Element("Amount").Attribute("Included").Value;
In place of the two ternary operators you use, I would recommend using (string) - the explicit cast will safely handle missing elements or attributes. If the element or attribute is missing, the code will not fail, it will simply have no value for that property.
So, putting this all together you get this:
XNamespace ns = "http://www.juniper.es/webservice/2007/";
var hotels = (from hotelData in data.Root.Descendants(ns + "HotelResult")
select new Hotel
{
Code = (string)hotelData.Attribute("Code"),
JpCode = (string)hotelData.Attribute("JPCode"),
DestinationZone = (string)hotelData.Attribute("DestinationZone"),
JpdCode = (string)hotelData.Attribute("JPDCode"),
HotelInfo = (from hi in hotelData.Descendants(ns + "HotelInfo")
select new HotelInfo
{
Name = (string)hi.Element("Name"),
Description = (string)hi.Element(ns + "Description"),
Image = (from img in hi.Descendants(ns + "Images")
select new Imagez
{
Images = (string)img.Element(ns + "Image")
}).ToList(),
HotelCategory = (string)hi.Element(ns + "Name"),
HotelType = (string)hi.Element(ns + "Description"),
Address = (string)hi.Element(ns + "Description"),
}).ToList(),
HotelOptions = (from ho in hotelData.Descendants(ns + "HotelOption")
select new HotelOptions
{
HotelOption = ho.Attribute("RatePlanCode").Value,
Board = ho.Element(ns + "Board").Attribute("Type").Value,
Prices = (from pr in ho.Descendants(ns + "Prices")
select new Price
{
Price = (string)pr.Element(ns + "Price"),
TotalFixAmounts = (from tfa in pr.Descendants(ns + "Price").Descendants(ns + "TotalFixAmounts")
select new TotalFixAmounts
{
Service = tfa.Element(ns + "Service").Attribute("Amount").Value,
ServiceTaxes = tfa.Element(ns + "ServiceTaxes").Attribute("Included").Value,
AmountServiceTaxes = tfa.Element(ns + "ServiceTaxes").Attribute("Amount").Value,
Commissions = tfa.Element(ns + "Commissions").Attribute("Included").Value,
AmountCommissions = tfa.Element(ns + "Commissions").Attribute("Amount").Value,
HandlingFees = tfa.Element(ns + "HandlingFees").Attribute("Included").Value,
AmountHandlingFees = tfa.Element(ns + "HandlingFees").Attribute("Amount").Value
}).ToList(),
}).ToList(),
}).ToList(),
}).ToList();
Note that every reference to Descendants or Element has the namespace ns in it.
I'm trying to write an RSS feed for the Ferrari site, but I can't read it all, using SyndicationItem. The problem is that the method getContent() reads only <link>,<title> and <description> elements (all of them are children of <item>). I need to read also the content of the <textnews> element, child of <item>. I must modify this query:
feed.LoadFromXml(feedXML);
// Query LINQ per effettuare il mapping
return feed.Items.ToList().Select(x =>
new FeedItem
{
Title = x.Title.ExtractText(),
Uri = x.GetUri(),
Id = (x.ItemUri != null) ? x.ItemUri.ToString() : x.Title.ExtractText(),
PubDate = x.PublishedDate.ToLocalTime().DateTime,
Content = x.GetContent(),
PlainTextContent = x.GetContent().ToPlainText(),
Subtitle = string.Empty,
ContentType = x.GetContentType(),
}).ToList();
Can you help me?
You'd need to parse the document as an XDocument, then parse each node. Eg:
var doc = XDocument.Load(aString);
var news = doc.Element("textnews").InnerText;
I am trying to add a sub category to Messages in my xml statement Is there a way I can do this GroupMessages -> Message -> GroupMessage :
var groups = xDoc.Descendants("Group")
.Select(n => new
{
GroupName = n.Element("GroupName").Value,
GroupHeader = n.Element("GroupHeader").Value,
TimeCreated = DateTime.Parse(n.Element("TimeAdded").Value),
Tags = n.Element("Tags").Value,
Messages = n.Element("GroupMessages").Value
//line above
})
.ToList();
dataGrid2.ItemsSource = groups;
In my method GroupMessages contains both MessageID and GroupMessage and it is listing both in my datagrid within the one container. So I tried this but it lists nothing:
Messages = n.Descendants("GroupMessages").Select(nd => nd.Element("GroupMessage").Value)
My XML looks like this:
<Group>
<TimeAdded>2012-04-27T10:23:50.7153613+01:00</TimeAdded>
<GroupName>Group</GroupName>
<GroupHeader>Header</GroupHeader>
<GroupMessages>
<Message>
<MessageID>1</MessageID>
<GroupMessage>Message</GroupMessage>
<MessageGroup/>
</Message>
</GroupMessages>
</Group>
I have also tried:
Messages = n.Descendants("GroupMessages").Select(nd => nd.Descendants("Message").Select(nde => nde.Element("GroupMessage").Value))
To no avail?
Update:
private void ListGroups_Click(object sender, RoutedEventArgs e)
{
string uriGroup = "http://localhost:8000/Service/Group";
XDocument xDoc = XDocument.Load(uriGroup);
var groups = xDoc.Descendants("Group")
.Select(n => new
{
GroupName = n.Element("GroupName").Value,
GroupHeader = n.Element("GroupHeader").Value,
TimeCreated = n.Element("TimeAdded").Value,
Tags = n.Element("Tags").Value,
Messages = n.Element("GroupMessages").Descendants("Message").Select(nd => new
{
//Id = nd.Element("MessageID").Value,
Message = nd.Element("GroupMessage").Value
}).FirstOrDefault()
})
.ToList();
dataGrid2.ItemsSource = groups;
}
Unfortunatley this method shows "Collection" inside the cell in the datagrid. If I try ToArray it will show an array message inside the cell. Is there a way to actually display the GroupMessage? Not sure how you set the child elements of a datagrid?
At the most basic level, you can do this to get a single message (the first one):
var groups = from grp in xDoc.Descendants("Group")
select new {
GroupName = grp.Element("GroupName").Value,
GroupHeader = grp.Element("GroupHeader").Value,
TimeCreated = DateTime.Parse(grp.Element("TimeAdded").Value),
Message = grp.Element("GroupMessages").Element("Message").Element("GroupMessage").Value
};
However, I assume that you want Messages to be a list of messages with both ID and Message. In that case, consider this:
var groups = from grp in xDoc.Descendants("Group")
select new {
GroupName = grp.Element("GroupName").Value,
GroupHeader = grp.Element("GroupHeader").Value,
TimeCreated = DateTime.Parse(grp.Element("TimeAdded").Value),
Messages = grp.Element("GroupMessages")
.Descendants("Message")
.Select(msg => new {
Id = msg.Element("MessageID").Value,
Message = msg.Element("GroupMessage").Value
}).ToList()
};
However, I strongly stress that all this usage of anonymous classes is just going to cause confusion. If you have a class for Group and Message then use those.
Note that the problem you're having is you're ignoring the XML structure and selecting random elements. To get the value out of a single element, you're going to need to select exactly that element, and ask for .Value. Selecting it's parent, or it's parent's parent (as you did) is not enough.
Try this:
var groups = xDoc.Elements("Group")
.Select(n => new
{
GroupName = n.Get("GroupName", string.Empty),
GroupHeader = n.Get("GroupHeader", string.Empty),
TimeCreated = n.Get("TimeAdded", DateTime.MinValue),
Tags = n.Get("Tags", string.Empty),
Messages = n.GetEnumerable("GroupMessages/Message", m => new
{
Id = m.Get("MessageID", 0),
Message = m.Get("GroupMessage", string.Empty),
Group = m.Get("MessageGroup", string.Empty)
}).ToArray()
})
.ToList();
I used the extension methods from here: http://searisen.com/xmllib/extensions.wiki
Get will handle null cases like your Tags node that doesn't exist in your xml, as well as the empty tag MessageGroup. You should use Elements() if the node you want is the child of the node you are referencing and not any descendant by that name.
I copied your xml into a root node to test it. It works on this xml:
<root>
<Group>
<TimeAdded>2012-04-27T10:23:50.7153613+01:00</TimeAdded>
<GroupName>Group</GroupName>
<GroupHeader>Header</GroupHeader>
<GroupMessages>
<Message>
<MessageID>1</MessageID>
<GroupMessage>Message</GroupMessage>
<MessageGroup/>
</Message>
</GroupMessages>
</Group>
<Group>
<TimeAdded>2012-04-27T10:23:50.7153613+01:00</TimeAdded>
<GroupName>Group</GroupName>
<GroupHeader>Header</GroupHeader>
<GroupMessages>
<Message>
<MessageID>1</MessageID>
<GroupMessage>Message</GroupMessage>
<MessageGroup/>
</Message>
</GroupMessages>
</Group>
</root>
I have an xml that looks like
<response>
<book>
<title>Harry Potter</title>
<Price>
<Currency>USD</Currency>
<Amount>$19.89</Amount>
</Price>
</book>
</response>
I have no problem getting the title element, but when I'm trying to get all values within price, it doesn't work.
var prices = from price in xmlDoc.Descendants("Price")
select new
{
currency = price.Element("currency").Value,
amount = price.Element("amount").Value,
};
Given your XML snippet, this code can populate a sequence of objects
var books = from book in document.Descendants("book")
let title = book.Element("title").Value
let price = book.Element("Price")
let currency = price.Element("Currency").Value
let amount = price.Element("Amount").Value
select new
{
Title = title,
Price = new
{
Currency = currency,
Amount = amount
}
};
This structure follows the same hierarchy as established by the XML. You can, of course, flatten it into a single level if you wish.
I changed the casing of the currency and amount elements to title casing and it worked fine.
var prices =
from price in xmlDoc.Descendants("Price")
select new
{
currency = price.Element("Currency").Value,
amount = price.Element("Amount").Value,
};
XML is case-sensitive.