Solved

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

Posted on 2014-11-10
4
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 63

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 63

Expert Comment

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

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

732 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