2016-06-14 108 views
5

我正在使用代碼項目中的代碼將xml文件拆分爲多個文件。正是在這樣的下面情況下工作正常:「註冊記憶」是父節點,當拆分是其中「註冊」.net C#分割xml文件

<Registrations> 
<Registration xmlns:i="..............."> 
    <RegistrationID>108260</RegistrationID> 
................... 
.................. 
</Registration> 
<Registration xmlns:i="..............."> 
    <RegistrationID>108260</RegistrationID> 
    ................... 
    .................. 
</Registration> 
<Registration xmlns:i="..............."> 
    <RegistrationID>108260</RegistrationID> 
    ................... 
    .................. 
</Registration> 

但是,當XML文件的格式如下不正常的代碼:「 RegistrationOpenData」是根節點,則存在另一個節點‘註冊記憶’和分割具有之中要取得‘註冊’

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

'XElement.Descendants'是遞歸的結果。我想你可能實際上正在尋找'XElement.Elements' – pquest

+0

我嘗試使用var childNodes = xml.Elements(「Registration」);而不是var childNodes = xml.Descendants(descendantElement);但它仍然不起作用。沒有結果。 – Karishma

+0

你能展示你期望達到的輸出嗎? –

回答

1

我剛剛在VS 2015中測試了你的代碼,它似乎工作。它生成3個包含以下內容的XML文件:

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

這是您所期待的嗎?你能提供關於你的問題的更多細節嗎?

+0

我正在使用VS 2012.我在此問題中指定的第一種格式是預期輸出。如果從XML文件父節點「RegistrationOpenData」被刪除,那麼代碼工作正常,文件被拆分。但是,如果「RegistrationOpenData」和「註冊」都存在,則什麼都不會發生。沒有錯誤,沒有分裂。 – Karishma

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

+0

我試過這個修改,但仍然無法正常工作。我正在使用VS2012。我希望事實並非如此。 – Karishma

+0

在我的情況下,代碼childNodes.Count()返回0,因此它甚至沒有進入while循環。 – Karishma

+0

嗨,我試圖在小提琴和它進入循環.https://dotnetfiddle.net/6sOOdH –