mprimmer
asked on
writing xml document c#.net 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!
<package><origination_id>1 </originat ion_id>
<package_key>00220091231</ package_ke y>
<email_flag>1</email_flag>
<file>
<grouping_level>002</group ing_level>
<statement_dt>2009-12-31</ statement_ dt>
<file_type>STMT</file_type >
<file_name>CP002STMT200912 31.pdf</fi le_name>
<file_size>94015</file_siz e>
</file><file>
<grouping_level>002</group ing_level>
<statement_dt>2009-12-31</ statement_ dt>
<file_type>INV</file_type>
<file_name>CP002INV2009123 1.pdf</fil e_name>
<file_size>82015</file_siz e>
</file></package>
-------------------------- ---------- ---------- --
foreach (DataRow dr in InvoicePkgsdt.Rows)
{
filename = prestageDirectory + "\\CP" + dr["credit_account_nmbr"]. ToString() .TrimEnd() + Convert.ToDateTime(dr["inv oice_dt"]) .ToString( "yyyyMMdd" ) + ".DDF";
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.Load(filename);
}
catch (System.IO.FileNotFoundExc eption)
{
//if file is not found, create a new xml file
XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8) ;
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteProcessingI nstruction ("xml", "version='1.0' encoding='UTF-8'");
xmlWriter.WriteStartElemen t("Root");
xmlWriter.Close();
xmlDoc.Load(filename);
}
XmlNode root = xmlDoc.DocumentElement;
XmlElement PackageElem = xmlDoc.CreateElement("pack age");
XmlElement OrigIdElem = xmlDoc.CreateElement("orig ination_id ");
XmlElement PackageKeyElem = xmlDoc.CreateElement("pack age_key");
XmlElement EmailElem = xmlDoc.CreateElement("emai l_flag");
XmlElement FileElem = xmlDoc.CreateElement("file ");
XmlElement GroupingLvlElem = xmlDoc.CreateElement("grou ping_level ");
XmlElement StatementDateElem = xmlDoc.CreateElement("stat ement_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_acc ount_nmbr" ].ToString ().TrimEnd () + Convert.ToDateTime(dr["inv oice_dt"]) .ToString( "yyyyMMdd" ));
XmlText EmailText = xmlDoc.CreateTextNode("0") ;
root.AppendChild(PackageEl em);
PackageElem.AppendChild(Or igIdElem);
OrigIdElem.AppendChild(Ori gIdText);
PackageElem.AppendChild(Pa ckageKeyEl em);
PackageKeyElem.AppendChild (PackageKe yText);
PackageElem.AppendChild(Em ailElem);
EmailElem.AppendChild(Emai lText);
DataView Reportsdv = new DataView(Reportsdt);
Reportsdv.RowFilter = "credit_account_nmbr = '" + dr["credit_account_nmbr"]. ToString()
+ "' and invoice_dt = '" + Convert.ToDateTime(dr["inv oice_dt"]) .ToString( ) + "'";
Reportsdv.Sort = "report_cd DESC";
XmlText GroupingLvlText = xmlDoc.CreateTextNode(dr[" credit_acc ount_nmbr" ].ToString ().TrimEnd ());
XmlText StatementDateText = xmlDoc.CreateTextNode(Conv ert.ToDate Time(dr["i nvoice_dt" ]).ToStrin g("MM/dd/y yyy"));
XmlText FileTypeText = xmlDoc.CreateTextNode("");
XmlText FileNameText = xmlDoc.CreateTextNode("");
XmlText FileSizeText = xmlDoc.CreateTextNode("");
string archiveDir = ArchiveDirectory + Convert.ToDateTime(dr["inv oice_dt"]) .ToString( "MMyyyy") + "\\";
for (int int_row = 0; int_row < Reportsdv.Count; int_row++)
{
PackageElem.AppendChild(Fi leElem);
FileElem.AppendChild(Group ingLvlElem );
GroupingLvlElem.AppendChil d(Grouping LvlText);
FileElem.AppendChild(State mentDateEl em);
StatementDateElem.AppendCh ild(Statem entDateTex t);
FileElem.AppendChild(FileT ypeElem);
FileTypeText.Value = Reportsdv[int_row]["report _file_node "].ToStrin g().TrimEn d();
FileTypeElem.AppendChild(F ileTypeTex t);
FileElem.AppendChild(FileN ameElem);
FileNameText.Value = Reportsdv[int_row]["FileNa me"].ToStr ing();
FileNameElem.AppendChild(F ileNameTex t);
FileElem.AppendChild(FileS izeElem);
FileInfo fi = new FileInfo(archiveDir + Reportsdv[int_row]["FileNa me"].ToStr ing());
FileSizeText.Value = fi.Length.ToString();
FileSizeElem.AppendChild(F ileSizeTex t);
File.Copy(archiveDir + Reportsdv[int_row]["FileNa me"], prestageDirectory + "\\" + Reportsdv[int_row]["FileNa me"]);
}
xmlDoc.Save(filename);
}
<package><origination_id>1
<package_key>00220091231</
<email_flag>1</email_flag>
<file>
<grouping_level>002</group
<statement_dt>2009-12-31</
<file_type>STMT</file_type
<file_name>CP002STMT200912
<file_size>94015</file_siz
</file><file>
<grouping_level>002</group
<statement_dt>2009-12-31</
<file_type>INV</file_type>
<file_name>CP002INV2009123
<file_size>82015</file_siz
</file></package>
--------------------------
foreach (DataRow dr in InvoicePkgsdt.Rows)
{
filename = prestageDirectory + "\\CP" + dr["credit_account_nmbr"].
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.Load(filename);
}
catch (System.IO.FileNotFoundExc
{
//if file is not found, create a new xml file
XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8)
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteProcessingI
xmlWriter.WriteStartElemen
xmlWriter.Close();
xmlDoc.Load(filename);
}
XmlNode root = xmlDoc.DocumentElement;
XmlElement PackageElem = xmlDoc.CreateElement("pack
XmlElement OrigIdElem = xmlDoc.CreateElement("orig
XmlElement PackageKeyElem = xmlDoc.CreateElement("pack
XmlElement EmailElem = xmlDoc.CreateElement("emai
XmlElement FileElem = xmlDoc.CreateElement("file
XmlElement GroupingLvlElem = xmlDoc.CreateElement("grou
XmlElement StatementDateElem = xmlDoc.CreateElement("stat
XmlElement FileTypeElem = xmlDoc.CreateElement("file
XmlElement FileNameElem = xmlDoc.CreateElement("file
XmlElement FileSizeElem = xmlDoc.CreateElement("file
XmlText OrigIdText = xmlDoc.CreateTextNode("1")
XmlText PackageKeyText = xmlDoc.CreateTextNode(dr["
XmlText EmailText = xmlDoc.CreateTextNode("0")
root.AppendChild(PackageEl
PackageElem.AppendChild(Or
OrigIdElem.AppendChild(Ori
PackageElem.AppendChild(Pa
PackageKeyElem.AppendChild
PackageElem.AppendChild(Em
EmailElem.AppendChild(Emai
DataView Reportsdv = new DataView(Reportsdt);
Reportsdv.RowFilter = "credit_account_nmbr = '" + dr["credit_account_nmbr"].
+ "' and invoice_dt = '" + Convert.ToDateTime(dr["inv
Reportsdv.Sort = "report_cd DESC";
XmlText GroupingLvlText = xmlDoc.CreateTextNode(dr["
XmlText StatementDateText = xmlDoc.CreateTextNode(Conv
XmlText FileTypeText = xmlDoc.CreateTextNode("");
XmlText FileNameText = xmlDoc.CreateTextNode("");
XmlText FileSizeText = xmlDoc.CreateTextNode("");
string archiveDir = ArchiveDirectory + Convert.ToDateTime(dr["inv
for (int int_row = 0; int_row < Reportsdv.Count; int_row++)
{
PackageElem.AppendChild(Fi
FileElem.AppendChild(Group
GroupingLvlElem.AppendChil
FileElem.AppendChild(State
StatementDateElem.AppendCh
FileElem.AppendChild(FileT
FileTypeText.Value = Reportsdv[int_row]["report
FileTypeElem.AppendChild(F
FileElem.AppendChild(FileN
FileNameText.Value = Reportsdv[int_row]["FileNa
FileNameElem.AppendChild(F
FileElem.AppendChild(FileS
FileInfo fi = new FileInfo(archiveDir + Reportsdv[int_row]["FileNa
FileSizeText.Value = fi.Length.ToString();
FileSizeElem.AppendChild(F
File.Copy(archiveDir + Reportsdv[int_row]["FileNa
}
xmlDoc.Save(filename);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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())
XmlElement FileElem = xmlDoc.CreateElement("file
ASKER
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!!
Things are working great now.... thanks for your help!!
ASKER
XmlElement FileElem = xmlDoc.CreateElement("file