Connecting Tech Pros Worldwide Forums | Help | Site Map

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

ALI-R
Guest
 
Posts: n/a
#1: Nov 16 '05
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=====



Dennis Myrén
Guest
 
Posts: n/a
#2: Nov 16 '05

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


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();
}

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"ALI-R" <newbie@microsoft.com> wrote in message
news:eY1pyP84EHA.1524@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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=====
>
>[/color]


ALI-R
Guest
 
Posts: n/a
#3: Nov 16 '05

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


thank you very much indeed ,it is exactly what I want.

"Dennis Myrén" <dennis@oslokb.no> wrote in message
news:I%xwd.415$IW4.7240@news2.e.nsc.no...[color=blue]
> 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();
> }
>
> --
> Regards,
> Dennis JD Myrén
> Oslo Kodebureau
> "ALI-R" <newbie@microsoft.com> wrote in message
> news:eY1pyP84EHA.1524@TK2MSFTNGP09.phx.gbl...[color=green]
> > 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[/color][/color]
XML[color=blue][color=green]
> > 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=====
> >
> >[/color]
>
>[/color]


Closed Thread