Solved

Read an XML file between a repetative nodes and create new xml files from it

Posted on 2014-11-10
4
179 Views
Last Modified: 2014-12-09
I have an xml file. Here I need to read the elements between ImportDocument starting and ending nodes and create a new xml document with those elements and its values. So, for the below xml file, I need to create 3 new xml files.

But before creating a new xml document I need to get values from database for Pernsr and SubmittingPernr element values.

Can anyone has any sample code for this task?

<?xml version="1.0" encoding="UTF-8"?>
-<DmHrImport xsi:noNamespaceSchemaLocation="http://dm-corp.swna.wdpr.disney.com/ImportSchemas/DmHrImport.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-<Feed>
      <Interface>000001</Interface>
      <Version>001</Version>
</Feed>
-<Options>
      <Test>N</Test>
      <BatchName>00000120140528502</BatchName>
</Options>
-<ImportDocument>
      <Pernr>00895722</Pernr>
      <SubmittingPernr>00895722</SubmittingPernr>
      <Type>APPLMAT</Type>
      <Application>ACROBAT</Application>
      <FilePath>\files\0000\00895722_1_Emergency Contact Information.pdf</FilePath>
      <DocumentDate>2014-05-15</DocumentDate>
      <Notes/>
</ImportDocument>
-<ImportDocument>
      <Pernr>00895722</Pernr>
      <SubmittingPernr>00895722</SubmittingPernr>
      <Type>APPLMAT</Type>
      <Application>ACROBAT</Application>
      <FilePath>\files\0000\00895722_2_Release of Info & Documentation(College).pdf</FilePath>
      <DocumentDate>2014-05-15</DocumentDate>
      <Notes/>
</ImportDocument>
-<ImportDocument>
      <Pernr>00891102</Pernr>
      <SubmittingPernr>00891102</SubmittingPernr>
      <Type>APPLMAT</Type>
      <Application>ACROBAT</Application>
      <FilePath>\files\0000\00891102_3_Emergency Contact Information.pdf</FilePath>
      <DocumentDate>2014-05-11</DocumentDate>
      <Notes/>
</ImportDocument>
</DmHrImport>
0
Comment
Question by:pothireddysunil
  • 2
4 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40433569
Hi pothireddysunil;

The following sample code should get you started.

// Load the XML file to be parsed into the system memory
var xdoc = XDocument.Load("C:/Working Directory/Export.xml");

// Query the XML document for all the ImportDocument elements and its child nodes 
// and place it into a List<XElement>
var results = (from node in xdoc.Root.Descendants("ImportDocument")
               select node).ToList();

// index is here to give the file names a unique name
int index = 1;
// Iterate over the List<XElement> that holds the ImportDocument nodes
foreach (XElement node in results)
{
    // Create a new XDocument and add the node as the Root node of the new document
    var doc = new XDocument(node);
    // Get the following two values from the XML to query database
    string Pernr = node.Element("Pernr").Value;
    string SubmittingPernr = node.Element("SubmittingPernr").Value;
    // 
    // Query the database and any other thing that needs to be done
    //
    // I am creating a file name here, please change as needed
    string saveAs = @"C:\Working Directory\" + Pernr + "-" + index.ToString() + ".xml";
    // Save the new document to the file system
    doc.Save(saveAs);
    index++;
}

Open in new window

0
 
LVL 2

Expert Comment

by:toakkiyadav
ID: 40435427
I suspect this is some kind of batch process and importdocument tags would be in good numbers.
Pernr and SubmittingPernr are look up values

My approach would be -
1. Query database and spit out an xml which will server as lookup xml.
2. Use XSLT to break batch xml file into multiple xml files.

Below url's should help you in achieving #2.
http://stackoverflow.com/questions/4181496/xslt-output-into-multiple-xml-files-based-on-grouping
http://stackoverflow.com/questions/19021205/merging-two-xml-files-using-xslt

thanks
0
 
LVL 1

Author Closing Comment

by:pothireddysunil
ID: 40489408
Thanks
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40489470
Not a problem, glad I was able to help.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question