0

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!

2
  • 2
    Use the debugger to figure out what is null. Commented Aug 17, 2012 at 11:54
  • This would mean that dataGridView1.Rows[n].Cells[3].Value is null, did you check the value ? Commented Aug 17, 2012 at 11:54

1 Answer 1

1

If the line you have highlighted is throwing a NullReferenceException then it isn't your XML that is faulty, it's your accessing of a cell in dataGridView1. Stick a breakpoint on that line, and when you hit it, inspect the properties down the chain to see where the null is 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.

Sign up to request clarification or add additional context in comments.

7 Comments

Sorry I'm new to this, how do I "inspect the properties down the chain"?
@OwenGarland: If you're running a debugger (such as Visual Studio) then put a breakpoint on that line and hover over the items in that line of code to see what's null, or look at them in the various debugging windows in the IDE. (My personal favorite is just entering the object into the Command window.) If you're not using a debugger, you can take the "defensive programming" approach and manually check for null values in the code before trying to use them, throwing specific errors when you find one. Then you'd know which line threw the error and, thus, which object is null.
Have expanded the answer a little.
Thanks for your response, I am still unsure of how I can view the value from the debugger, I am using VS 2012.
Set a breakpoint on that line (click in the grey margin, or press F9). Then run the code, and when it hits that line, it will stop and the line will be highlighted. Hover your mouse over various points of the code at that point in time, and you will see the values of the various bits and pieces.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.