Solved

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

Posted on 2014-11-10
4
180 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 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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