获取XML文件的部分内容的XDocument

3
我是一名有用的助手,可以为您进行翻译。以下是需要翻译的内容:

我有一个大型的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)不起作用(似乎仅适用于Cooperations标记而不是Cooperation标记)

有人可以指导一下吗。

谢谢,
rAyt

编辑:
背景是这样的:我通过一个 Web 服务将这些 XML 部分推送到 Blackberry。不幸的是,Blackberry 企业服务器上的 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>
  ...

512 kb 哈哈哈... 稍等一下,我要添加背景。 - Henrik P. Hessel
已找到并修复 - 请查看我的更新 - Marc Gravell
3个回答

2

如果您想使用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;

完全相同于:

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

(因为编译器忽略了明显的select,没有将其映射到Select LINQ方法)

嗨,马克,我已经尝试过了。但是那样会缺少开头和结尾的<cooperation>标签。 - Henrik P. Hessel
无法复现... 如果您的意思是缺少 <Cooperations> 标签... 那么请手动添加? - Marc Gravell
输出应该以<CooperationId>xxx开头,而不是<Cooperation>的开始标签。 - Henrik P. Hessel

1

您是否有一个XML文档或片段,即是否有多个“Cooperations”节点?如果有多个,您希望获取哪些合作关系?只是从一个“Cooperations”还是跨越多个?问这个问题的原因是您已经编写了xml.Elements("Cooperations")。

这样做不就可以了吗:

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

不行,只能有一个<Cooperations>标签。 - Henrik P. Hessel
谢谢,但这里也缺少合作标签。 - Henrik P. Hessel
foreach (var xx in x.Element("Cooperations").Elements("Cooperation").Skip(2).Take(3)) { Console.WriteLine(xx.Name); }给我“合作”*3 - veggerby

0
你可以使用System.Net而不是LINQ来实现这个,虽然会比较混乱。只是为了给你一个关于如何读取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();

您可以通过调整count并同时搜索XML标记str来使用分页。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接