score:4
Here is the code for a Console application with the LINQ query you need. I had to fill in the getters and setters of your class and containing structure, but this is tested working code. To get a List(Of ASNData) just call DataTables.ToList
Of course this will work with any number of Order elements.
Using Structure Order works just fine. I would use a Class, but no need to change it for this to work.
The key part of this code is the LINQ query:
Dim DataTables = From NewDataTable In TestData...<NewDataTable> _
Select New ASNData With {.Field = NewDataTable.<Field>.Value, _
.Value = NewDataTable.<Val>.Value, _
.Orders = (From AnOrder In NewDataTable...<Order> _
Select New ASNData.Order With _
{.Number = AnOrder.@Number, _
.ShippingDate = Date.Parse(AnOrder.@ShipDate)}).ToList}
Here is the complete working console app:
Module Module1
Sub Main()
Dim TestData = <NewDataSet>
<NewDataTable>
<Field>Accepted ASNs</Field>
<Val>59</Val>
<Order Number="1234" ShipDate="2009/05/21"/>
<Order Number="2190" ShipDate="2009/05/22"/>
<Order Number="1809" ShipDate="2009/05/22"/>
</NewDataTable>
<NewDataTable>
<Field>Rejected ASNs</Field>
<Val>8</Val>
<Order Number="2901" ShipDate="2009/05/21"/>
<Order Number="2810" ShipDate="2009/05/24"/>
<Order Number="1419" ShipDate="2009/05/25"/>
</NewDataTable>
<NewDataTable>
<Field>Missing ASNs</Field>
<Val>7</Val>
<Order Number="2902" ShipDate="2009/05/19"/>
<Order Number="2898" ShipDate="2009/05/20"/>
<Order Number="1296" ShipDate="2009/05/22"/>
</NewDataTable>
</NewDataSet>
Dim DataTables = From NewDataTable In TestData...<NewDataTable> _
Select New ASNData With {.Field = NewDataTable.<Field>.Value, .Value = NewDataTable.<Val>.Value, _
.Orders = (From AnOrder In NewDataTable...<Order> _
Select New ASNData.Order With {.Number = AnOrder.@Number, .ShippingDate = Date.Parse(AnOrder.@ShipDate)}).ToList}
Console.WriteLine(DataTables.Count)
Console.ReadLine()
End Sub
Public Class ASNData
Private _field As String
Private _value As String
Private _orders As List(Of Order)
Public Property Field()
Get
Return _field
End Get
Set(ByVal value)
_field = value
End Set
End Property
Public Property Value() As String
Get
Return _value
End Get
Set(ByVal value As String)
_value = value
End Set
End Property
Public Property Orders() As List(Of Order)
Get
Return _orders
End Get
Set(ByVal value As List(Of Order))
_orders = value
End Set
End Property
Structure Order
Private _number As String
Private _date As Date
Public Property Number() As String
Get
Return _number
End Get
Set(ByVal value As String)
_number = value
End Set
End Property
Public Property ShippingDate() As Date
Get
Return _date
End Get
Set(ByVal value As Date)
_date = value
End Set
End Property
End Structure
End Class
End Module
score:0
To get order elements (from a NewDataTable
element), you'd use
newDataTableElement.Elements("Order")
which will return an IEnumerable<XElement>
- you can then use Select
to transform each one as normal. To get the attributes, use:
element.Attribute("Number").Value
element.Attribute("ShipDate").Value
That will give the value to use as string - you can get XAttribute
to do the conversions for you though:
CType(element.Attribute("Number"), Integer)
CType(element.Attribute("ShipDate"), DateTime)
I'm not 100% that that will work for your dates, because it may expect a full date and time - worth a try though. Otherwise, just parse the string using DateTime.ParseExact
.
I would strongly recommend that you reconsider Order
being a structure though - particularly a mutable structure. Any reason for making it a structure rather than a class?
score:-1
var result = from order in YourXMLVar.Descendants("NewDataTable")
.First().Elements("Order") select order;
This should give you an Array of Orders.
Then you should be able to do something like:
foreach(var order in result)
{
int someVal = Convert.ToInt32(order.Attribute("Number").Value);
}
// EDIT: Obviously, you wouldn't use First(), but instead would iterate through your orders or whatever your logic dictates. I used First() because it made for an easy demo.
score:0
XDocument xmlDoc = XDocument.Load(Server.MapPath("Order.xml"));
List<ASNData> lst= (from data in xmlDoc.Root.Elements("NewDataTable")
select new ASNData
{
Field=data.Element("Field").Value,
Value=data.Element("Val").Value,
Orders=(from ord in data.Elements("Order")
select new Order
{
Number=ord.Attribute("Number").Value,
ShippingDate=Convert.ToDateTime(ord.Attribute("ShipDate").Value)
}).ToList<Order>()
}).ToList<ASNData>();
foreach(ASNData a in lst)
{
Response.Write("Field:"+a.Field+"</br>");
Response.Write("Value:"+a.Value+"</br>");
Response.Write("Orders:"+"</br>");
foreach(Order o in a.Orders)
{
Response.Write("OrderNum:"+o.Number+"</br>");
Response.Write("ShipDate:"+o.ShippingDate+"</br>");
}
}
Source: stackoverflow.com
Related Articles
- LINQ To XML Syntax for XML Element with Attributes
- LINQ method syntax to group by a column, pick one element from each group with total count per group
- C# LINQ to XML query with duplicate element names that have attributes
- LinQ GroupBy on nested element with same attributes
- Linq to XML: create an anonymous object with element attributes and values
- How does LINQ expression syntax work with Include() for eager loading
- Linq syntax for OrderBy with custom Comparer<T>
- GroupBy with linq method syntax (not query syntax)
- How to remove an element from an xml using Xdocument when we have multiple elements with same name but different attributes
- get last element with linq to sql
- Syntax to execute code block inside Linq query?
- How to update an element with a List using LINQ and C#
- Is there some sort of syntax error with this LINQ JOIN?
- LINQ question ... need to get element with min value
- Checking if a XML child element exists with Linq to XML
- How do I do IN syntax with Linq
- Using LINQ to delete an element from a ObservableCollection Source
- JOIN and LEFT JOIN equivalent in LINQ with Method Syntax
- LINQ Source Code Available
- Issues with LINQ statement with defining an element of a collection
- LINQ Method Syntax with INNER and OUTER Join
- Linq with where clause in many-to-many EF Code First object
- linq join operator type with lambda syntax
- I need a LINQ expression to find an XElement where the element name and attributes match an input node
- Add a LINQ or DBContext extension method to get an element if not exist then create with data in predicate (FirstOrCreate)
- How to access element from jArray with Linq
- Problems trying to use GroupBy with multiple properties using the LINQ Method Syntax
- C# linq to xml, nested query with attributes
- Convert linq query expression with multiple froms into extension method syntax
- Read attributes values with linq
- Delete all related rows to user using EF .NET CORE
- LINQ multiple and or condition
- What is the best way to separate string using string.format() function or LINQ ?
- using group by in IEnumerable result
- LINQ Determine if a property on an object in a collection exists in a different collection
- Collection was modified; enumeration operation may not execute during select
- Use LINQ to get datatable row numbers meeting certain conditions
- EF How to query more entities with .include() and using repository pattern
- Is there a way to select two values into same IEnumerable using LINQ in C#?
- Conditional OR has no effect in Linq-to-entities?
- Get Reports for next 10 days in single database hit
- Entity Framework Include and Select are gettind some other entities
- Entity Framework Sum() performance
- How to get Linq data context for this function?
- String was not recognized as a valid DateTime - Whats wrong?
- Linq, use "variable" inside a anonymous type
- EF Core dynamic filter
- How do we retrieve the first item of a WhereSelectListIterator?
- Combining two adjacent collections to create Json rows
- Confused on Getting an int out of a var from Linq2Sql