write xml file from data table ignoring columns with white spaces



I am trying to export the data from a data table into xml file. I have this part working but when a record does not have any data or a white space it still writes the it in the xml file with XML:space Preserved.


I want to ignor the columns and not have them in xml file if they do not have any data in them


example of the xml file it is producing now


this is the current output


I want customer street 3 and 4 nodes to be not printed if they don't have any values in them.


here is my code



using System.IO;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml.Linq;
using System;
using System.Collections.Generic;
using PdfSharp.Pdf.IO;
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using System.Xml;


namespace InvoicePrintProgram


{ class XMLGenerator {



//Defining method that generates XMl Files
public void Start(String XmlFilepath, string XMlFileName, DataTable DT, int PageCountOut, int SequenceCountOut, int[] PrefIndex/*, int[] SequenceIndex, IEnumerable<String> chunk, int IndexCount out int IndexCountOut*/)
{
// Creates Xml file from datatable using the wrtieXml method

FileStream streamWrite = new FileStream(XmlFilepath, System.IO.FileMode.Create);


System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings();
settings.Indent = true;
//settings.Encoding = System.Text.Encoding.GetEncoding("ISO-8859-1")
settings.Encoding = System.Text.Encoding.UTF8;
settings.CloseOutput = true;
settings.CheckCharacters = true;
settings.NewLineChars = "\r\n";
// vbCr & vbLf

// Write as UTF-8 with indentation




DT.WriteXml(streamWrite, XmlWriteMode.IgnoreSchema);




// foreach (DataRow dr in DT.Rows)
// {
// foreach (DataColumn col in DT.Columns)
// {

// dr[col] = dr[col].ToString().Replace(" ", "").Trim();




// }
// }

//DT.WriteXml(streamWrite);
//streamWrite.Close();



XElement xEle = XElement.Load(XmlFilepath);
// xEle = XElement.Parse(XmlFilepath, LoadOptions.PreserveWhitespace);

// using Linq refines the file by adding the metadata details and removes the statementnumbers from XML file.
xEle.AddFirst(new XElement("PdfMetaData", new XElement("fileName", XMlFileName)
, new XElement("TotalPageCount", PageCountOut)
, new XElement("TotalPackageCount", SequenceCountOut)));
// }
xEle.Save(XmlFilepath);


// Adds the pageIndex at the bottom of the XML file
//xEle.Add(new XElement("PrefStockPageIndexes"));

//for (int i = 0; i <= chunk.Count() - 1; i++)
//{

// xEle.Add(new XElement("int", SequenceIndex[IndexCount]));
// IndexCount++;
//}
// IndexCountOut = IndexCount;
// public void FindFirstPage(PdfDocument document, String XmlFilePath, string referenceString)
// {
// this procedure removes any pages from the pdf document that do not contain
// the reference string







xEle.Add(new XElement("PrefStockPageIndexes"));

for (int i = 0; i <= PrefIndex.Length - 1; i++)
{

xEle.Add(new XElement("int", PrefIndex[i]));

}
xEle.Save(XmlFilepath);
// }

}
}


}


No comments:

Post a Comment