Export reach row as XML with Elements

tdignan87

Well-known member
Joined
Jul 8, 2019
Messages
95
Programming Experience
Beginner
Hi everyone,
I have just found this forum! looking forward to checking this place out.

I have a datagridview (unbound) that exports successfully into XML with elements. I want to however; run the XML for every line in the datagridview that contains data and treat it as a individual XML import. Basically the code to loop for the next row.
See example for code.

try
{

foreach (DataGridViewRow row in dataGridView1.Rows)

{



XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;


// Initialize the XmlWriter.
// Dim XmlWrt As XmlWriter = XmlWriter.Create("COMMODITY.xml", settings)
XmlWriter XmlWrt = XmlWriter.Create(ExportLocTxt.Text, settings);
{
var withBlock = XmlWrt;

// Write the Xml declaration.
withBlock.WriteStartDocument();

// Write a comment.
withBlock.WriteComment("Stevens Container Import. iSchedulr");

// Write the root element.
withBlock.WriteStartElement("stevens");

// Start our first person.
withBlock.WriteStartElement("containers");


withBlock.WriteStartElement("container");

// The person nodes.



withBlock.WriteStartElement("code");
withBlock.WriteString(dataGridView1.CurrentRow.Cells[0].Value.ToString());

// withBlock.WriteString(row.Cells[0].Value.ToString());
withBlock.WriteEndElement();



withBlock.WriteStartElement("name");
withBlock.WriteString(dataGridView1.CurrentRow.Cells[1].Value.ToString());
// withBlock.WriteString(row.Cells[1].Value.ToString());
withBlock.WriteEndElement();

withBlock.WriteStartElement("description");
withBlock.WriteString(dataGridView1.CurrentRow.Cells[2].Value.ToString());
// withBlock.WriteString(row.Cells[2].Value.ToString());
withBlock.WriteEndElement();

withBlock.WriteStartElement("tare");
withBlock.WriteString(dataGridView1.CurrentRow.Cells[3].Value.ToString());
// withBlock.WriteString(row.Cells[3].Value.ToString());
withBlock.WriteEndElement();

withBlock.WriteStartElement("type");
withBlock.WriteString(dataGridView1.CurrentRow.Cells[5].Value.ToString());
// withBlock.WriteString(row.Cells[5].Value.ToString());
withBlock.WriteEndElement();

withBlock.WriteStartElement("group");
withBlock.WriteString(dataGridView1.CurrentRow.Cells[4].Value.ToString());
// withBlock.WriteString(row.Cells[4].Value.ToString());
withBlock.WriteEndElement();

withBlock.WriteStartElement("location");
withBlock.WriteString(dataGridView1.CurrentRow.Cells[6].Value.ToString());
// withBlock.WriteString(row.Cells[6].Value.ToString());
withBlock.WriteEndElement();

// The end of this person.
withBlock.WriteEndElement();

withBlock.WriteEndElement();

// Close the XmlTextWriter.
withBlock.WriteEndDocument();
withBlock.Close();
 
Sorry its not that i am not putting in the effort; I am learning and trying my best. I will get there.....
Yeah i am familiar with writing IF statements.
I will give that a bash; and update my findings.

Thank you for your patience with this newbie!
 
If i have "allow user to add rows" = disabled then there is no errors
To exclude the "new row" row you can check row.IsNewRow.

If a cell.Value can be null you can't do .ToString directly, null.ToString will cause NullReferenceException. One option is to use the null conditional operator like this value?.ToString(), this will return null if the value is null and that is ok to use as input for WriteElementString, or else the value as string. Another could be to convert using Convert.ToString method instead.
 
Back
Top Bottom