XDocument获取XML文件的一部分 [英] XDocument Get Part of XML File

查看:53
本文介绍了XDocument获取XML文件的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大型的xml文件,并希望从其中获取定义数量的< Cooperation> 节点.处理此问题的最佳方法是什么?

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();
}

跳过(部分* chunksize).Take(chunksize)不起作用(似乎仅对合作标签有效,而对合作标签无效)

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.

谢谢,
拉伊特


背景是这样的:我正在通过Web服务将这些xml部件推送到Blackberry.不幸的是,黑莓企业服务器上的HTTP请求大小受到限制默认为256 kb.


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.

XML文件的一部分:

<?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>
  ...

推荐答案

对于使用 XDocument ,我希望您想要这样的东西:

For using XDocument, I expect you want something like:

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

但是,如果数据是,则可能必须下拉至 XmlReader .我将尝试做一个示例...(更新; 512kb可能不值得...)

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...)

您的代码存在的问题是您在此处使用 .Elements():

The problem with your code is that you are using .Elements() here:

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

只需删除它:

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;

100%等同于:

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

(因为编译器会忽略明显的 select ,而不将其映射到 Select LINQ方法)

(since the compiler ignores an obvious select, without mapping it to the Select LINQ method)

这篇关于XDocument获取XML文件的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆