我正在构建一个xml文件。部分文件是静态的。一些文件是动态的。我的代码有一个“空对象引用”的错误。Linq XML Dynamic building
任何提示将是真棒。
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);
}
我看不出为什么你会在该代码中得到NullReferenceException。请提供一个简短但完整的程序来说明问题。 – 2009-10-19 16:29:32
您提供的代码在LINQPad中运行得很好。也许你正在向TestXML传递一个空文件名? – StriplingWarrior 2009-10-19 16:51:59