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, ie 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.Element s ("Cooperations").
Wouldn't this do the trick:
xml.Element("Cooperations").Elements("Cooperation").Skip(...).Take(...)
You could do this by using System.Net
instead of LINQ, although it would be quite messy. Just to give you an idea on how you can read parts of a http response:
// Get the HTTP response
string url = "http://someurl.com/myxml.xml";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Build a stream
Stream stream = response.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader reader = new StreamReader( stream, encode );
// Loop the file
Char[] read = new Char[256];
int count = reader.Read( read, 0, 256 );
while (count > 0) {
String str = new String(read, 0, count);
count = reader.Read(read, 0, 256);
}
response.Close();
stream.Close();
You can use paging by adjusting the count
and simultaneously searching str
for XML tags.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.