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:
var qry = doc.Root.Elements("Cooperation").Skip(part*chunksize).Take(chunksize);
however, if the data is large, you might have to drop down to XmlReader
instead... 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:
foreach (XElement x in childList.Elements())
{
output_xml.Append(x.ToString());
}
Just remove that:
foreach (XElement x in childList)
{
output_xml.Append(x.ToString());
}
For info - you are also using query syntax unnecessarily:
IEnumerable<XElement> childList = from el in xml.Elements("Cooperations")
.Skip(part * chunksize).Take(chunksize) select el;
is 100% identical to:
IEnumerable<XElement> childList = xml.Elements("Cooperations")
.Skip(part * chunksize).Take(chunksize);
(since the compiler ignores an obvious select
, without mapping it to the Select
LINQ method)
Do you have an xml document or a fragment, i.e do you have more than 1 "Cooperations" nodes? If you have more, which Coopertation's are you expecting to get? From just 1 Cooperations or across multiple, reason for asking is that you have written xml.Elements("Cooperations").
Wouldn't this do the trick:
xml.Element("Cooperations").Elements("Cooperation").Skip(...).Take(...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With