.net C#Spliting xml文件

时间:2016-06-14 11:07:44

标签: c# .net 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>

但是当XML文件采用这种格式时,代码无效:&#34; RegistrationOpenData&#34;是根节点,然后有另一个节点&#34;注册&#34;并且必须在&#34;注册&#34;

之间进行拆分
<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>

我使用的代码如下:

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

2 个答案:

答案 0 :(得分:1)

我刚刚在VS 2015中测试了您的代码,它似乎有效。它生成3个XML文件,其中包含以下内容:

<?xml version="1.0" encoding="utf-8"?>
<RegistrationOpenData>
  <Registration>
    <RegistrationID>108260</RegistrationID>
  </Registration>
</RegistrationOpenData>

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

答案 1 :(得分:0)

作为一种快速解决方案(我假设您不想在您的codeproject脚本中进行更改),您可以添加以下行:

private static void SplitXmlFile(string sourceFile
                    , string rootElement
                    , string descendantElement
                    , int takeElements
                    , string destFilePrefix
                    , string destPath)
{ 
    XElement xml = XElement.Load(sourceFile);
    XNamespace ns = "http://services.hpd.gov"; // This line must be added.
    xml = xml.Element(ns + rootElement); // rootElement must be "Registrations". And also this line must be added.
    // Child elements from source file to split by.
    var childNodes = xml.Descendants(ns + descendantElement);
.....
.....

这是工作样本:https://dotnetfiddle.net/6sOOdH