C# linqxml动态构建
我正在构建一个xml文件。文件的某些部分是静态的。有些文件是动态的。我的代码有一个“空对象引用”错误 任何提示都会很棒C# linqxml动态构建,c#,xml,linq,linq-to-xml,C#,Xml,Linq,Linq To Xml,我正在构建一个xml文件。文件的某些部分是静态的。有些文件是动态的。我的代码有一个“空对象引用”错误 任何提示都会很棒 private XElement BuildDataElement() { // this is going to be more complicated return new XElement("data"); } public void TestXML(string fname) { // build the data element XE
private XElement BuildDataElement()
{
// this is going to be more complicated
return new XElement("data");
}
public void TestXML(string fname)
{
// build the data element
XElement allData = BuildDataElement();
// Build the header
XDocument doc = new XDocument(
new XElement("map",
new XAttribute("showLabels", "1"),
new XAttribute("includeNameInLabels", "1"),
new XElement("colorRange",
new XElement("color",
new XAttribute("minValue", "1")
)
),
allData,
new XElement("application",
new XElement("apply",
new XAttribute("toObject", "TOOLTIP"),
new XAttribute("styles", "TTipFont,MyDataPlotStyle")
)
)
)
);
if (File.Exists(fname))
File.Delete(fname);
doc.Save(fname);
}
在提供的代码片段中,我可以看到出现错误的唯一方法是两个位置
BuildDataElement();
可能正在生成错误,而不是Xml文档
下一步如果
BuildDataElement()
返回这可能是问题所在,因为我猜XDocument正在执行.ToString()
或对所有数据执行某些操作在提供的代码段中,我可以看到出现错误的唯一方法是两个位置
BuildDataElement();
可能正在生成错误,而不是Xml文档
下一步如果BuildDataElement()
返回可能是问题所在,因为我猜XDocument正在对所有数据执行操作
任何提示都会很棒
private XElement BuildDataElement()
{
// this is going to be more complicated
return new XElement("data");
}
public void TestXML(string fname)
{
// build the data element
XElement allData = BuildDataElement();
// Build the header
XDocument doc = new XDocument(
new XElement("map",
new XAttribute("showLabels", "1"),
new XAttribute("includeNameInLabels", "1"),
new XElement("colorRange",
new XElement("color",
new XAttribute("minValue", "1")
)
),
allData,
new XElement("application",
new XElement("apply",
new XAttribute("toObject", "TOOLTIP"),
new XAttribute("styles", "TTipFont,MyDataPlotStyle")
)
)
)
);
if (File.Exists(fname))
File.Delete(fname);
doc.Save(fname);
}
你明白了。以下是我的建议:
- 获取调试器
- 将调试器设置为在所有异常时中断李>
- 在调试器中运行代码,直到发生null引用异常李>
- 找出您不希望为null的null值
- 要么插入一个null检查,以正确处理值为null的情况,要么更改逻辑,使该值不可能为null
- 彻底检查代码并测试修复程序
- 为您的测试套件编写一个回归测试,以验证此bug不会再次出现
任何提示都会很棒
private XElement BuildDataElement()
{
// this is going to be more complicated
return new XElement("data");
}
public void TestXML(string fname)
{
// build the data element
XElement allData = BuildDataElement();
// Build the header
XDocument doc = new XDocument(
new XElement("map",
new XAttribute("showLabels", "1"),
new XAttribute("includeNameInLabels", "1"),
new XElement("colorRange",
new XElement("color",
new XAttribute("minValue", "1")
)
),
allData,
new XElement("application",
new XElement("apply",
new XAttribute("toObject", "TOOLTIP"),
new XAttribute("styles", "TTipFont,MyDataPlotStyle")
)
)
)
);
if (File.Exists(fname))
File.Delete(fname);
doc.Save(fname);
}
你明白了。以下是我的建议:
- 获取调试器
- 将调试器设置为在所有异常时中断李>
- 在调试器中运行代码,直到发生null引用异常李>
- 找出您不希望为null的null值
- 要么插入一个null检查,以正确处理值为null的情况,要么更改逻辑,使该值不可能为null
- 彻底检查代码并测试修复程序
- 为您的测试套件编写一个回归测试,以验证此bug不会再次出现
我不明白为什么在代码中会出现NullReferenceException。请提供一个简短但完整的程序来演示这个问题。您提供的代码在LINQPad中运行良好。也许您正在将一个空文件名传递到TestXML中?我不明白为什么在该代码中会出现NullReferenceException。请提供一个简短但完整的程序来演示这个问题。您提供的代码在LINQPad中运行良好。可能您正在将空文件名传递到TestXML?同意,请先用调试器找出错误所在的行,而不是发布整个方法。同意,请先用调试器找出错误所在的行,而不是发布整个方法。