how to create xml files out of an xml file based on an element

A

ALI-R

Hi All,

I have an xml file with the structure below.I'd like to create dynamic
number of xml files from this file based on <subreport> elements.each XML
file contains
subreport element only .for instance if there are 4 <subreport> elements
,I'd like to create 4 different xml files and I want them to be named based
on <Ship_Via> child value in each <subreport> element.

Is there somebody who can give me an idea?
thanks for your help.
ALI


====XML file====
<?xml version="1.0"?>
<report>
<subreport>
<heading>
<Sales_Order_Lbl>Sales Order1</Sales_Order_Lbl>
<Ship_Via>10-TR</Ship_Via>
</heading>
<detail_values>
<Ln>2</Ln>
<Item_Number>12</Item_Number>
<Producer_Address>Address1:</Producer_Address>
</detail_values>
<footer>
<Total_Plts_Lbl>Total2</Total_Plts_Lbl>
<Total_Invoice>0.00</Total_Invoice>
</footer>
</subreport>
<subreport>
<heading>
<Sales_Order_Lbl>Sales Order2</Sales_Order_Lbl>
<Ship_Via>11-TR</Ship_Via>
</heading>
<detail_values>
<Ln>2</Ln>
<Item_Number>13</Item_Number>
<Producer_Address>Address2:</Producer_Address>
</detail_values>
<footer>
<Total_Plts_Lbl>Total2</Total_Plts_Lbl>
<Total_Invoice>0.00</Total_Invoice>
</footer>
</subreport>
</report>

=====End of XML file=====
 
D

Dennis Myrén

Something like:

XmlDocument sourceDoc = new XmlDocument();
sourceDoc.Load(sourceFile);
#if GUARANTEED_ONLY_SUBREPORT_NODES_PRESENT_UNDER_ROOT
XmlNodeList subreportNodes = sourceDoc.DocumentElement.ChildNodes;
#else
XmlNodeList subreportNodes =
sourceDoc.DocumentElement.SelectNodes("subreport");
#endif
foreach (XmlNode subreportNode in subreportNodes)
{
XmlTextWriter xout = new XmlTextWriter
(string.Concat(OUTPUT_PATH,
subreportNode.SelectSingleNode("heading/Ship_Via").InnerText, ".xml",
Encoding.UTF8);
xout.Formatting = Formatting.Indented;
xout.WriteStartDocument(true);
subreportNode.WriteTo(xout);
xout.Close();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top