C#xmlAttribute.Value对象引用未设置为对象的实例

C#xmlAttribute.Value对象引用未设置为对象的实例,c#,xmldocument,C#,Xmldocument,早上好 很快 使用XmlDocument我正在编程创建文档,它需要如下所示(示例): 也 带下划线(VS2010编辑器)的提示: Possible 'System.NullReferenceException' 但我认为这就是上述问题的原因 感谢您的支持,并提前感谢您的时间和知识共享。 致以最良好的祝愿 我还没有尝试运行所示的代码,但是:您可能会发现,简化代码会使出错变得更加困难: XmlElement customerNode; // <==== note this is XmlEle

早上好

很快

使用XmlDocument我正在编程创建文档,它需要如下所示(示例):

带下划线(VS2010编辑器)的提示:

Possible 'System.NullReferenceException'
但我认为这就是上述问题的原因

感谢您的支持,并提前感谢您的时间和知识共享。


致以最良好的祝愿

我还没有尝试运行所示的代码,但是:您可能会发现,简化代码会使出错变得更加困难:

XmlElement customerNode; // <==== note this is XmlElement, not XmlNode
XmlAttribute customerAttribute;

foreach (DataGridViewRow row in dgvCustomers.Rows)
{
    customerNode = doc.CreateElement("customer");
    customerNode.SetAttribute("id", row.Cells[0].Value.ToString());
    customerNode.SetAttribute("storeId", row.Cells[1].Value.ToString());

    customersNode.AppendChild(customerNode);
}

XmlElement自定义节点;// 我没有试过运行所示的代码,但是:您可能会发现,简化代码会使出错变得更加困难:

XmlElement customerNode; // <==== note this is XmlElement, not XmlNode
XmlAttribute customerAttribute;

foreach (DataGridViewRow row in dgvCustomers.Rows)
{
    customerNode = doc.CreateElement("customer");
    customerNode.SetAttribute("id", row.Cells[0].Value.ToString());
    customerNode.SetAttribute("storeId", row.Cells[1].Value.ToString());

    customersNode.AppendChild(customerNode);
}

XmlElement自定义节点;//您确定
行.单元格[0]
有值(不是nul)吗?检查debugQuick行。单元格[0]。对其进行赋值,并查看其是否有值请参阅我的上一条评论。非常感谢。您确定
行.单元格[0]
有值(不是nul)吗?检查debugQuick行。单元格[0]。对其进行赋值,并查看其是否有值请参阅我的上一条评论。非常感谢。好的,明白了,将数据加载到DGV的方法会在包含行的最后一个数据下面添加空行。它抛出了一个例外:)添加了检查,效果很好,谢谢大家!好的,明白了,将数据加载到DGV的方法会在包含行的最后一个数据下面添加空行。它抛出了一个例外:)添加了检查,效果很好,谢谢大家!
Possible 'System.NullReferenceException'
XmlElement customerNode; // <==== note this is XmlElement, not XmlNode
XmlAttribute customerAttribute;

foreach (DataGridViewRow row in dgvCustomers.Rows)
{
    customerNode = doc.CreateElement("customer");
    customerNode.SetAttribute("id", row.Cells[0].Value.ToString());
    customerNode.SetAttribute("storeId", row.Cells[1].Value.ToString());

    customersNode.AppendChild(customerNode);
}