score:2
Accepted answer
you are trying to select response
elements from response
element (which is root of your xml). use this element directly instead:
var responseelement = xelement.load(path_to_xml);
var statuselement = responseelement.element("status");
var myobject = new marketclockresponse
{
response = new response
{
id = (string)responseelement.attribute("id"),
date = (string)responseelement.element("date"),
message = (string)responseelement.element("message"),
status = new status
{
current = (string)statuselement.element("current"),
change_at = (string)statuselement.element("change_at")
}
}
};
score:1
var myobject = xelem.descendants("response").select(
x => new marketclockresponse
{
response = new response
{
id = x.attribute("id").value,
//.....
//populate all the attributes
}
});
score:1
first of all, i would use xdocument.load
instead of xelement.load
, because your xml is a document, with declaration, etc.
var xdoc = xdocument.load("input.txt");
then, i'd set two local variables to avoid querying for the same thing more than once:
var resp = xdoc.root;
var status = resp.element("status");
and use them to get what you need:
var myobject = new marketclockresponse
{
response = new response
{
id = (string)resp.attribute("id"),
date = (string)resp.element("date"),
message = (string)resp.element("message"),
status = new status
{
current = (string)status.element("current"),
change_at = (string)status.element("change_at")
}
}
};
Source: stackoverflow.com
Related Query
- LINQ Source Code Available
- creating Linq to sqlite dbml from DbLinq source code
- How to flatten a multi level XML into a single level XML using c# code LINQ
- Linq to XML Converter
- Null reference exception in my LINQ to XML code
- source code for LINQ 101 samples
- I need to save the XML file using linq with xml code in C#
- C# LINQ code not working for XML parse
- Construct a list of wpf Hyperlink elements from an XML source file using Linq
- C# Linq to XML Element.Remove Code Optimization
- Adding a condition/filter to this LINQ to XML code
- c# Linq or code to extract groups from a single list of source data
- Using LINQ on XML data source
- Linq to xml with missing nodes in the source XML and null-coalescing operator won't work
- How can convert Xml code to linq
- How to load xml code block into existing xml file at a specific node using linq
- Convert string[] to int[] in one line of code using LINQ
- LINQ to read XML
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Linq code to select one item
- Ignore namespaces in LINQ to XML
- How are people unit testing code that uses Linq to SQL
- Empty namespace using Linq Xml
- C# Linq to XML check if element exists
- Reading XML using XDocument & Linq - check if element is NULL?
- De/Serialize directly To/From XML Linq
- Linq to XML - update/alter the nodes of an XML Document
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Linq to Xml : Exception -The ' ' character, hexadecimal value 0x20, cannot be included in a name
- How/Can I use linq to xml to query huge xml files with reasonable memory consumption?
More Query from same tag
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- Filtering non-generic DbSet with dynamically built Expression
- Use index as an extra id in linq query
- Filter EntityDataSource on Association value
- How to add a PredicateBuilder to my left outer join
- LINQ expression with generic class properties
- LINQ query using EF Core throws translation exception
- Blank csv file with Linq/ASP.Net/MVC
- multiple query with linq
- Getting and Setting SQL Timeout?
- Linq Query with Any() using in memory object and db
- Inner method in Linq query that return IQueryable
- Lambda Expression With Contains Not Working
- Why does IEnumerable.Any return True for a collection of False-booleans?
- EF. Select records from one table based on the properties of the records from second table that have an FK from the first
- Implement C# Linq Distinct Help in order to group a column in table
- linq to sql "All" operator type query
- Query MongoDB 'ObjectId' in C# Linq Driver
- Sort and Group in LINQ
- LINQ? Refactoring foreach
- Update the data in database in asp.net
- C# Entity Framework CS1941
- Entity Navigation Property IQueryable cannot be translated into a store expression
- Related entities not loading when using Include with Linq to Entities
- extract id attributes strings
- LINQ find MAX value of set of values within a larger set
- Will Linq ToList() generate NEW items?
- LINQ: how to get 1 month records
- LINQ: get access to child lists during runtime
- C# Linq expressions can't loop through data results?