Link to home
Start Free TrialLog in
Avatar of mprimmer

asked on

writing xml document loop overlaying elements rather than appending

I'm trying to create an xml document and am having a problem with my loop to create my sub nodes called 'file'.  I am trying to make an xml doc much like the one below.  I have a set of file names that I loop through and get some additional data on and then create an xml set called <file> within the main <package> set.  Subsequently, if I have 10 files, I should have 10 <file> entries.  See below.  It's looping through however rather than create a new entry, it is overlaying the first one.  so when I get all done, I have the last file only.  Below is my code.  I acquired this code from the net somewhere and modified it so I haven't completely come to understand it all just yet and it may need some further modifications as I work it through but for the most part, my xml doc is looking pretty good except for the reuse of rather than adding  of a new <file> set.  The second loop in the code is where I am writing the file elements. Please advise.  Thank you!


foreach (DataRow dr in InvoicePkgsdt.Rows)
      filename = prestageDirectory + "\\CP" + dr["credit_account_nmbr"].ToString().TrimEnd() + Convert.ToDateTime(dr["invoice_dt"]).ToString("yyyyMMdd") + ".DDF";

      XmlDocument xmlDoc = new XmlDocument();

      catch (System.IO.FileNotFoundException)
      //if file is not found, create a new xml file
      XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8);
      xmlWriter.Formatting = Formatting.Indented;
                                                xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");

XmlNode root = xmlDoc.DocumentElement;
XmlElement PackageElem = xmlDoc.CreateElement("package");
XmlElement OrigIdElem = xmlDoc.CreateElement("origination_id");
XmlElement PackageKeyElem = xmlDoc.CreateElement("package_key");
XmlElement EmailElem = xmlDoc.CreateElement("email_flag");
XmlElement FileElem = xmlDoc.CreateElement("file");
XmlElement GroupingLvlElem = xmlDoc.CreateElement("grouping_level");
XmlElement StatementDateElem = xmlDoc.CreateElement("statement_dt");
XmlElement FileTypeElem = xmlDoc.CreateElement("file_type");
XmlElement FileNameElem = xmlDoc.CreateElement("file_name");
XmlElement FileSizeElem = xmlDoc.CreateElement("file_size");

XmlText OrigIdText = xmlDoc.CreateTextNode("1");
XmlText PackageKeyText = xmlDoc.CreateTextNode(dr["credit_account_nmbr"].ToString().TrimEnd() + Convert.ToDateTime(dr["invoice_dt"]).ToString("yyyyMMdd"));
XmlText EmailText = xmlDoc.CreateTextNode("0");






DataView Reportsdv = new DataView(Reportsdt);
                                    Reportsdv.RowFilter = "credit_account_nmbr = '" + dr["credit_account_nmbr"].ToString()
                                          + "' and invoice_dt = '" + Convert.ToDateTime(dr["invoice_dt"]).ToString() + "'";

Reportsdv.Sort = "report_cd DESC";

XmlText GroupingLvlText = xmlDoc.CreateTextNode(dr["credit_account_nmbr"].ToString().TrimEnd());
XmlText StatementDateText = xmlDoc.CreateTextNode(Convert.ToDateTime(dr["invoice_dt"]).ToString("MM/dd/yyyy"));
XmlText FileTypeText = xmlDoc.CreateTextNode("");
XmlText FileNameText = xmlDoc.CreateTextNode("");
XmlText FileSizeText = xmlDoc.CreateTextNode("");

string archiveDir = ArchiveDirectory + Convert.ToDateTime(dr["invoice_dt"]).ToString("MMyyyy") + "\\";
for (int int_row = 0; int_row < Reportsdv.Count; int_row++)



      FileTypeText.Value = Reportsdv[int_row]["report_file_node"].ToString().TrimEnd();

      FileNameText.Value = Reportsdv[int_row]["FileName"].ToString();

      FileInfo fi = new FileInfo(archiveDir + Reportsdv[int_row]["FileName"].ToString());
      FileSizeText.Value = fi.Length.ToString();

      File.Copy(archiveDir + Reportsdv[int_row]["FileName"], prestageDirectory + "\\" + Reportsdv[int_row]["FileName"]);



Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mprimmer


I think that's right but I'm not sure how to dynamically alter the object name for the create element statement.  I want to concatenate the int_row loop variable onto the word "FileElem".  so I can reference FileElem1, fileelem2, ectc.... can't seem to get the syntax right to get it going.  Can you help me?

XmlElement FileElem = xmlDoc.CreateElement("file")

If you want to include the loop variable as part of the tagname in your CreateElement call, try this:

XmlElement FileElem = xmlDoc.CreateElement("file" + int_row.ToString())
Thank you... that last question wasn't quite what I meant but it was a stupid move on my part anyway.
   Things are working great now.... thanks for your help!!