Xml

Apr 20, 2009 at 7:59 AM

It would help to include an Xml example to the sample project.
I have added one example below, but it needs to be improved.

Regards
Jan S.

 * Add the code below to Form1.cs in the sample project.
* You need to change Taxable = 1 to Taxable = 'true' in the word template for the Xml example to work.
   Needed because I don't want to use an Xsd file, causing all columns to be of type string.
* Add a new merge button called 'Merge using Xml'.
* Call the function buttonMergeWithXml_Click then the button is pressed.

 

 

// You need to change Taxable = 1 to Taxable = 'true' in the word template for the Xml example,
// or add an XSD that defines Taxable as an integer.
private void buttonMergeWithXml_Click(object sender, EventArgs e)
{
DataSet dsReport = new DataSet();
string sXml = GetXmlData();
StringReader srXml = new StringReader(sXml);
dsReport.ReadXml(srXml);
foreach (DataTable dtRecord in dsReport.Tables)
{
string strTableName = dtRecord.TableName;
string strPrimaryKey = "";
if (dtRecord.PrimaryKey.Length > 0)
{
strPrimaryKey = dtRecord.PrimaryKey[0].ColumnName;
}

string strParentkey = "";
foreach (DataRelation dr in dtRecord.ParentRelations)
{
if (dtRecord.ParentRelations[0].ChildColumns.Length > 0)
{
strParentkey = dtRecord.ParentRelations[0].ChildColumns[0].ColumnName;
}
}

this.MailMerge.AddRegionData(dtRecord, strTableName/*"PackItems"*/, strPrimaryKey/*"PackItems_Id"*/, strParentkey/*"OrderItems_Id"*/);
}

// Set document-level data
if (dsReport.Tables["OrderData"] != null)
{
this.MailMerge.DocumentData = dsReport.Tables["OrderData"].Rows[0];
}

try
{
this.MailMerge.Merge();
}
catch (System.Exception Exc)
{
MessageBox.Show(Exc.Message + "\r\nYou need to change Taxable = 1 to Taxable = 'true' in the word template for the Xml example.");
}

// Save the document to the output file
this.Doc.Save(this.OutputFileName.Text);
this.MergeButton.Enabled = false;
}

string GetXmlData()
{
string sXml = "<?xml version=\"1.0\" standalone=\"yes\"?>"
+ "<OrderData>"
+ "<OrderId>1</OrderId>"
+ "<DeliveryAddress>2 Queen St</DeliveryAddress>"
+ "<DeliverySuburb>Melbourne</DeliverySuburb>"
+ "<DeliveryPostcode>3000</DeliveryPostcode>"
+ "<InvoiceAddress>2 Queen St</InvoiceAddress>"
+ "<InvoiceSuburb>Melbourne</InvoiceSuburb>"
+ "<InvoicePostcode>3000</InvoicePostcode>"
+ "<OrderGroups>"
+ "<Title>Food</Title>"
+ "<OrderDeliveryTimes>"
+ "<Time>10.00am</Time>"
+ "<OrderItems>"
+ "<ItemTitle>Bagel</ItemTitle>"
+ "<Notes>With cream cheese</Notes>"
+ "<Quantity>12</Quantity>"
+ "<Taxable>true</Taxable>"
+ "</OrderItems>"
+ "<OrderItems>"
+ "<ItemTitle>Fruit Juice</ItemTitle>"

+ "<Notes>Orange &amp; Pineapple</Notes>"

+ "<Quantity>8</Quantity>"

+ "<Taxable>false</Taxable>"

+ "</OrderItems>"

+ "</OrderDeliveryTimes>"

+ "<OrderDeliveryTimes>"

+ "<Time>12.30pm</Time>"

+ "<OrderItems>"

+ "<ItemTitle>Lunch Pack</ItemTitle>"

+ "<Notes/>"

+ "<Quantity>15</Quantity>"

+ "<Taxable>true</Taxable>"

+ "<PackItems>"

+ "<ItemTitle>Sandwich</ItemTitle>"

+ "<Notes>White &amp; Wholemeal mix</Notes>"

+ "<Quantity>15</Quantity>"

+ "</PackItems>"

+ "<PackItems>"

+ "<ItemTitle>Fruit</ItemTitle>"

+ "<Notes>Apples &amp; Oranges</Notes>"

+ "<Quantity>15</Quantity>"

+ "</PackItems>"

+ "</OrderItems>"

+ "<OrderItems>"

+ "<ItemTitle>Fruit &amp; Cheese Platter</ItemTitle>"

+ "<Notes/>"

+ "<Quantity>3</Quantity>"

+ "<Taxable>false</Taxable>"

+ "</OrderItems>"

+ "</OrderDeliveryTimes>"

+ "<OrderDeliveryTimes>"

+ "<Time>10.00am</Time>"

+ "<OrderItems>"

+ "<ItemTitle>Napkins</ItemTitle>"

+ "<Notes>Linen</Notes>"

+ "<Quantity>15</Quantity>"

+ "<Taxable>true</Taxable>"

+ "</OrderItems>"

+ "<OrderItems>"

+ "<ItemTitle>Cutlery</ItemTitle>"

+ "<Notes>Silver</Notes>"

+ "<Quantity>15</Quantity>"

+ "<Taxable>true</Taxable>"

+ "</OrderItems>"

+ "</OrderDeliveryTimes>"

+ "</OrderGroups>"

+ "<OrderGroups>"

+ "<Title>Equipment</Title>"

+ "<OrderDeliveryTimes>"

+ "<Time>12.30pm</Time>"

+ "<OrderItems>"

+ "<ItemTitle>Plates</ItemTitle>"

+ "<Notes>Ceramic</Notes>"

+ "<Quantity>15</Quantity>"

+ "<Taxable>true</Taxable>"

+ "</OrderItems>"

+ "</OrderDeliveryTimes>"

"</OrderGroups>"
"</OrderData>";
return sXml;
}