2016-06-14 16 views
5

Sto utilizzando un codice da Code Project per dividere un file xml in più file. Si sta lavorando bene in questo caso al di sotto: "Registrazioni" è il nodo padre e quando spaccatura è tra "Registrazione".net C# Splitting file xml

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

Ma il codice non funziona quando il file XML è in questo formato: " RegistrationOpenData" è il nodo principale, poi c'è un altro nodo 'Registrazioni' e divisa deve essere fatta tra 'registrazione'

<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> 

il codice che sto usando è come qui sotto:

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' è ricorsivo nei suoi risultati. Penso che potresti effettivamente cercare 'XElement.Elements' – pquest

+0

Ho provato a utilizzare var childNodes = xml.Elements (" Registration "); invece di var childNodes = xml.Descendants (discendantElement); ma non funziona ancora. Non dare risultati – Karishma

+0

Puoi mostrare quale output ti aspetti di ottenere? –

risposta

1

Ho appena testato il codice in VS 2015 e sembra funzionare. Genera 3 file XML con il seguente contenuto:

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

E 'quello che ti aspetti? Puoi dare maggiori dettagli sul tuo problema? campione

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

qui sta lavorando::

+0

Sto usando VS 2012. Il primo formato che ho specificato in questa domanda è l'output atteso. Se dal file XML Nodo padre "RegistrationOpenData" viene rimosso, il codice funziona correttamente e il file viene diviso. Ma se entrambi "RegistrationOpenData" e "Registrations" sono presenti, non succede nulla. Nessun errore, nessuna divisione. – Karishma

0

come una soluzione rapida (presumo non si desidera apportare modifiche nello script CodeProject) è possibile aggiungere questa linea https://dotnetfiddle.net/6sOOdH

+0

Ho provato questa modifica ma ancora non funziona. Sto usando VS2012. Spero che non sia questo il caso. – Karishma

+0

Nel mio caso il codice childNodes.Count() restituisce 0 quindi non sta nemmeno entrando nel ciclo while. – Karishma

+0

ciao, l'ho provato in fiddle e sta entrando in loop.https: //dotnetfiddle.net/6sOOdH –