.net C#Spliting xml文件

Kar*_*hma 5 .net c# xml

我正在使用Code Project中的代码将xml文件拆分为多个文件.它在以下情况下正常工作:"注册"是父节点,当分割是在"注册"之间

<Registrations>
<Registration xmlns:i="...............">
  <RegistrationID>108260</RegistrationID>
...................
..................
 </Registration>
 <Registration xmlns:i="...............">
  <RegistrationID>108260</RegistrationID>
   ...................
   ..................
 </Registration>
 <Registration xmlns:i="...............">
  <RegistrationID>108260</RegistrationID>
   ...................
   ..................
 </Registration>
Run Code Online (Sandbox Code Playgroud)

但是当XML文件采用这种格式时代码不起作用:"RegistrationOpenData"是根节点,然后有另一个节点"Registrations"并且必须在"Registration"中进行拆分

<RegistrationOpenData xmlns:i="............" xmlns="">
<Description>......</Description>
<InformationURL>..........</InformationURL>
<SourceAgency>...............</SourceAgency>
<SourceSystem>...........</SourceSystem>
<StartDate>................</StartDate>
<EndDate i:nil="true" />
<Registrations>
<Registration xmlns:i="...............">
  <RegistrationID>108260</RegistrationID>
...................
..................
 </Registration>
 <Registration xmlns:i="...............">
  <RegistrationID>108260</RegistrationID>
...................
..................
 </Registration>
 <Registration xmlns:i="...............">
  <RegistrationID>108260</RegistrationID>
...................
..................
 </Registration>
</Registrations>
</RegistrationOpenData>
Run Code Online (Sandbox Code Playgroud)

我使用的代码如下:

private void buttonSPLIT_Click(object sender, EventArgs e)
{
    string sourceFile = @"D:\sample.xml";
    string rootElement = "RegistrationOpenData";
    string descElement = "Registration";
    int take = 1;
    string destFilePrefix = "RegistrationsPart";
    string destPath = @"D:\PART\";

    SplitXmlFile(sourceFile, rootElement, descElement, take,
        destFilePrefix, destPath);

}

private static void SplitXmlFile(string sourceFile
                    , string rootElement
                    , string descendantElement
                    , int takeElements
                    , string destFilePrefix
                    , string destPath)
{          
    XElement xml = XElement.Load(sourceFile);
    // Child elements from source file to split by.
    var childNodes = xml.Descendants(descendantElement);

    // This is the total number of elements to be sliced up into 
    // separate files.
    int cnt = childNodes.Count();

    var skip = 0;
    var take = takeElements;
    var fileno = 0;

    // Split elements into chunks and save to disk.
    while (skip < cnt)
    {
        // Extract portion of the xml elements.
        var c1 = childNodes.Skip(skip)
                        .Take(take);

        // Setup number of elements to skip on next iteration.
        skip += take;
        // File sequence no for split file.
        fileno += 1;
        // Filename for split file.
        var filename = String.Format(destFilePrefix + "_{0}.xml", fileno);
        // Create a partial xml document.
        XElement frag = new XElement(rootElement, c1);
        // Save to disk.
        frag.Save(destPath + filename);
    }
}
Run Code Online (Sandbox Code Playgroud)

Arn*_*lay 1

我刚刚在 VS 2015 中测试了你的代码,它似乎可以工作。它生成 3 个 XML 文件,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<RegistrationOpenData>
  <Registration>
    <RegistrationID>108260</RegistrationID>
  </Registration>
</RegistrationOpenData>
Run Code Online (Sandbox Code Playgroud)

这是你所期待的吗?您能否提供有关您的问题的更多详细信息?