Thanks for taking the time to possibly help me out!
I am currently having two problems, and I think that one is causing the other. Basically I am trying to insert an extra node into an existing XML file, and then fill it with data from a dataGridView. Here is the XML I am trying to change, i need to add the “Language” tag:
<stentry>
<index>28</index>
<sid>PARAM_TITLE1</sid>
<val>
<en>Param 1</en>
<es>parámetro 1</es>
***<Language>String<language>***
</val>
<params>
<fontref>187</fontref>
<numref>0</numref>
<clip>FALSE</clip>
<include>FALSE</include>
<protected>FALSE</protected>
<cwidth>-1</cwidth>
<dwidth>0</dwidth>
</params>
</stentry>
However I am also faced with the issue that the tag is used elsewhere:
<module>
..
<color>
<name>DIALOG</name>
<val>ffd4dbee</val>
<id>41a</id>
</color>
<color>
<name>WIDGET_FILL</name>
<val>ffc0c0c0</val>
<id>41c</id>
</color>
..
</module>
Currently I am using this method to insert the data:
int n = 0;
XmlNodeList nodeList = xDoc.GetElementsByTagName("val");
foreach (XmlNode node in nodeList)
{
if (node.OuterXml.Contains("val"))
{
XmlElement newElement = xDoc.CreateElement(tag);
**string data = dataGridView1.Rows[n].Cells[3].Value.ToString();**
XmlText txtVal = xDoc.CreateTextNode(data);
XmlNode parent1 = node.ParentNode;
parent1.AppendChild(newElement);
parent1.LastChild.AppendChild(txtVal);
n++;
}
}
The problem being that at the moment I get a NullReferenceException on the highlighted line. I believe this is because there is data being put into the incorrect tags ahead of where it needs to be. So when it tries to put data in where it is needed the value is null. But I am not 100% on this, so I have come seeking help.
Thanks a lot!
If the line you have highlighted is throwing a
NullReferenceExceptionthen it isn’t your XML that is faulty, it’s your accessing of a cell indataGridView1. Stick a breakpoint on that line, and when you hit it, inspect the properties down the chain to see where thenullis coming. This should help you see where the exact problem is. You can do this by hovering over each property in turn when the breakpoint is on that line.