[英]Linq XML Dynamic building
I am building an xml file. 我正在构建一个xml文件。 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);
}
Any tips would be awesome. 任何提示都会很棒。
You got it. 你说对了。 Here are my tips: 这是我的提示:
The only way in the snippet provided I can see getting an error would be 2 two places. 提供的片段中,我可以看到得到错误的唯一方法是2个2位。
BuildDataElement();
Could be generating the error, rather than the Xml document. 可能是生成错误,而不是Xml文档。
Next if BuildDataElement();
下一步,如果BuildDataElement();
returns that might be the problem, since I am guessing the XDocument is doing a .ToString()
or some action on allData
返回可能是问题所在,因为我猜XDocument正在对allData
执行.ToString()
或某些操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.