Can someone guide me on to repairing the error on this query :
var objApps = from item in xDoc.Descendants(\"VHost\")
where
The compiler is complaining about this part
from x in item.Descendants("Application")
inside your Where
clause. You should change it so that
select
clause at the end, andtrue
for item
objects that you would like to keep.Here is my best guess at what you are trying to do (EDIT : attempt number two)
var objApps = from item in xDoc.Descendants("VHost").Descendants("Application")
select new clsApplication {
ConnectionsTotal = item.Element("ConnectionsTotal").Value
};
I like the procedural way of linq which can be more linear in its approach; I believe you want
var result = xDoc.Descendants("VHost")
.Descendants("ConnectionsTotal")
.Select(ele => ele.Value )
.Select( value => new clsApplication
{
ConnectionsTotal = value
})
;
--- Test in LinqPad ---
string xml = @"
<Data>
<Server>
<ConnectionsCurrent>67</ConnectionsCurrent>
<ConnectionsTotal>1424182</ConnectionsTotal>
<ConnectionsTotalAccepted>1385091</ConnectionsTotalAccepted>
<ConnectionsTotalRejected>39091</ConnectionsTotalRejected>
<MessagesInBytesRate>410455.0</MessagesInBytesRate>
<MessagesOutBytesRate>540146.0</MessagesOutBytesRate>
</Server>
<VHost>
<Name>_defaultVHost_</Name>
<TimeRunning>5129615.178</TimeRunning>
<ConnectionsLimit>0</ConnectionsLimit>
<ConnectionsCurrent>67</ConnectionsCurrent>
<ConnectionsTotal>1424182</ConnectionsTotal>
<ConnectionsTotalAccepted>1385091</ConnectionsTotalAccepted>
<ConnectionsTotalRejected>39091</ConnectionsTotalRejected>
<MessagesInBytesRate>410455.0</MessagesInBytesRate>
<MessagesOutBytesRate>540146.0</MessagesOutBytesRate>
</VHost>
<Application>
<Name>TestApp</Name>
<Status>loaded</Status>
<TimeRunning>411642.953</TimeRunning>
<ConnectionsCurrent>11</ConnectionsCurrent>
<ConnectionsTotal>43777</ConnectionsTotal>
<ConnectionsTotalAccepted>43135</ConnectionsTotalAccepted>
<ConnectionsTotalRejected>642</ConnectionsTotalRejected>
<MessagesInBytesRate>27876.0</MessagesInBytesRate>
<MessagesOutBytesRate>175053.0</MessagesOutBytesRate></Application>
</Data>";
var XDoc = XDocument.Parse(xml);
XDoc.Descendants("VHost")
.Descendants("ConnectionsTotal")
.Select (ele => ele.Value )
.Dump();
The error is complaining about your inner from clause (inside the where) it looks like you're trying to do a select many (unintentional clippy reference).
var objApps = from item in xDoc.Descendants("VHost")
from x in item.Descendants("Application")
select new clsApplication
{
ConnectionsTotal = x.Element("ConnectionsTotal").Value
};
You are missing select in query within where clause, you can change it something like
var objApps = from item in xDoc.Descendants("VHost")
where(item.Descendants("Application").Any())
select new clsApplication
{
ConnectionsTotal = item.Element("Application").Element("ConnectionsTotal").Value
};
Simply do the following, no need for the where
clause. Descendants will search children at ALL levels, not just immediately below:
var objApps = from item in xDoc.Descendants("Application")
select new clsApplication
{
ConnectionsTotal = item.Element("ConnectionsTotal").Value
};
This may help:
var objApps = from item in xDoc.Descendants("VHost")
from x in item.Descendants("Application")
select new clsApplication
{
ConnectionsTotal = item.Element("Application").Element("ConnectionsTotal").Value
};