c# - XDocument 获取 XML 文件的一部分

标签 c# xml

我有一个很大的 xml 文件,想要获得定义数量的 <Cooperation>从它的节点。处理此问题的最佳方法是什么。

目前,我正在使用这段代码

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)不起作用(似乎只对合作标签有效,对合作标签无效)

有人能指出我正确的方向吗。

谢谢,
雷伊特

编辑:
背景是这样的:我正在通过网络服务将这些 xml 部分推送到黑莓手机。不幸的是,黑莓企业服务器上的 http 请求大小是有限的 默认为 256 kb。

部分 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 的使用,我希望你想要这样的东西:

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 方法)

关于c# - XDocument 获取 XML 文件的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1232397/

相关文章:

用于 64 位操作系统的 C#?

java - 如何将JSON转换为指定的XML格式

java - 在Android项目中,单个文件有R错误

xml - 解析 GML 编码的 WFS 响应 XML 文件

c# - 有多个根元素。 1号线,位置369

c# - 这是在 linq 中插入关系的正确方法吗

c# - Selenium 无法通过 xpath 按文本查找元素

c# - 为什么不能同时定义隐式和显式运算符?

c# - 产品限位计数器

php - 使用字符串将 XML 注入(inject)节点