Reputation: 14409
I am building an xml file. Parts of the file are static. Some of the file is dynamic. My code has an error of “Null object reference”.
Any tips would be awesome.
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);
}
Upvotes: 1
Views: 680
Reputation: 660493
Any tips would be awesome.
You got it. Here are my tips:
Upvotes: 7
Reputation: 73351
The only way in the snippet provided I can see getting an error would be 2 two places.
BuildDataElement();
Could be generating the error, rather than the Xml document.
Next if BuildDataElement();
returns that might be the problem, since I am guessing the XDocument is doing a .ToString()
or some action on allData
Upvotes: 0