XDocument gets part of an XML file

I have a large xml file and you want to get a certain number of nodes from it <Cooperation>. What is the best way to handle this.

I am currently 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 apply only for the Cooperations tag, not for collaboration tags)

Can someone point me in the right direction.

Thanks
Rayt

Edit:
In the background this is: I am pushing these parts of xml through webservice on Blackberry. Unfortunately, the size of the HTTP request on the BlackBerry enterprise server is limited to 256 kb by default.

XML file part:

<?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>
  ...
+3
source share
3 answers

XDocument, , - :

var qry = doc.Root.Elements("Cooperation").Skip(part*chunksize).Take(chunksize);

, , , , XmlReader ... ... (; 512kb, , ...)

, .Elements() :

foreach (XElement x in childList.Elements())
{
    output_xml.Append(x.ToString());
}

:

foreach (XElement x in childList)
{
    output_xml.Append(x.ToString());
}

- :

IEnumerable<XElement> childList = from el in xml.Elements("Cooperations")
    .Skip(part * chunksize).Take(chunksize) select el;

100% :

IEnumerable<XElement> childList = xml.Elements("Cooperations")
    .Skip(part * chunksize).Take(chunksize);

( select, select LINQ)

+2

xml- , .. 1 "Cooperations"? , , ? 1 , xml.Element s ( "Cooperations" ).

:

xml.Element("Cooperations").Elements("Cooperation").Skip(...).Take(...)
+1

You can do this using System.NetLINQ instead, although it will be pretty messy. Just to give you an idea of ​​how you can read parts of the answer on http:

// 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 customizing countand simultaneously searching strfor XML tags.

0
source

Source: https://habr.com/ru/post/1714391/


All Articles