I have a large xml file and want to get a defined number of <Cooperation> nodes from it. What’s the best way to handle this.
Currently, I’m using this code
public string FullCooperationListChunkGet(int part, int chunksize)
{
StringBuilder output_xml = new StringBuilder();
IEnumerable<XElement> childList = from el in xml.Elements("Cooperations").Skip(part * chunksize).Take(chunksize) select el;
foreach (XElement x in childList.Elements())
{
output_xml.Append(x.ToString());
}
return output_xml.ToString();
}
Skip(part * chunksize).Take(chunksize) doesn’t work (seems to be only valid for the Cooperations Tag and not the Cooperation Tags)
Can somebody point me in the right direction.
Thanks,
rAyt
Edit:
The Background is this: I’m pushing these xml parts via a webservice to a Blackberry. Unfortunately, the http request size on a blackberry enterprise server is limited
to 256 kb by default.
Part of the XML File:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Cooperations>
<Cooperation>
<CooperationId>xxx</CooperationId>
<CooperationName>xxx</CooperationName>
<LogicalCustomers>
<LogicalCustomer>
<LogicalCustomerId>xxx</LogicalCustomerId>
<LogicalCustomerName>xxx</LogicalCustomerName>
<Customers>
<Customer>
<CustomerId>xxx</CustomerId>
<CustomerName>xxx/CustomerName>
</Customer>
<Customer>
<CustomerId>xxx</CustomerId>
<CustomerName>xxx</CustomerName>
</Customer>
</Customers>
</LogicalCustomer>
<LogicalCustomer>
<LogicalCustomerId>xxx</LogicalCustomerId>
<LogicalCustomerName>xxx</LogicalCustomerName>
<Customers>
<Customer>
<CustomerId>xxx</CustomerId>
<CustomerName>xxx</CustomerName>
</Customer>
<Customer>
<CustomerId>xxx</CustomerId>
<CustomerName>xxx</CustomerName>
</Customer>
</Customers>
</LogicalCustomer>
<LogicalCustomer>
<LogicalCustomerId>xxx</LogicalCustomerId>
<LogicalCustomerName>xxx</LogicalCustomerName>
<Customers>
<Customer>
<CustomerId>xxx</CustomerId>
<CustomerName>xxx</CustomerName>
</Customer>
</Customers>
</LogicalCustomer>
</LogicalCustomers>
</Cooperation>
<Cooperation>
...
For using
XDocument, I expect you want something like:however, if the data is large, you might have to drop down to
XmlReaderinstead… I’ll try to do an example… (update; 512kb probably isn’t worth it…)The problem with your code is that you are using
.Elements()here:Just remove that:
For info – you are also using query syntax unnecessarily:
is 100% identical to:
(since the compiler ignores an obvious
select, without mapping it to theSelectLINQ method)