XDocument获取XML文件的一部分

时间:2009-08-05 10:34:22

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

有人可以指出我正确的方向。

谢谢,
rAyt

编辑:
背景是这样的:我正在通过web服务将这些xml部分推送到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>
  ...

3 个答案:

答案 0 :(得分: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;

与:100完全相同:

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

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

答案 1 :(得分:1)

你有一个xml文档或片段,即你有超过1个“合作”节点吗?如果你有更多,你期望得到哪些Coopertation?从1个合作或多个合作中,询问的原因是你写了xml.Element s (“合作”)。

这不会起到作用:

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

答案 2 :(得分: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并同时搜索str XML标记来使用分页。

相关问题