0
这里的问题我有一个XML文件,我试图追加数据。我使用LINQ to XML和我使用的代码如下:LINQ to XML追加数据抛出空引用异常
public void AppendSalesXMLData(Company company)
{
string FileName = "TestSales";
string OrgID = company.OrgID.ToString();
string SaleID = company.OrgSales[company.OrgSales.Count - 1].SaleID.ToString();
if (!File.Exists(String.Format(@"C:\Data-Source\trunk\Applications\VintageSiteInspector\XML\{0}.xml", FileName)))
{
CreateXMLFile(FileName);
}
XDocument thisDoc = XDocument.Load(String.Format(@"C:\Data-Source\trunk\Applications\VintageSiteInspector\XML\{0}.xml", FileName));
<!------- The following line throws an exception every time. ----->
thisDoc.Element(FileName).Add(new XElement("Sale"));
thisDoc.Save(String.Format(@"C:\Data-Source\trunk\Applications\VintageSiteInspector\XML\{0}.xml", FileName));
}
,我打开XML文件是
<?xml version="1.0" encoding="utf-8"?>
<root>
<TestSales></TestSales>
</root>
我只是不明白为什么我得到一个空参考例外。
哪些对象/哪一部分调用的例外呢? – 2012-01-10 15:50:46
@JamieKeeling <! - 下面一行每行都会抛出一个异常---> :) – CBRRacer 2012-01-10 15:51:52
假设你的文件名是“Test”,那么xml中是否有一个名为“Test”的元素?如果你试图添加一个XElement到XML,那么我想你需要做一些像thisDoc.Add(new XElement(“Sale”)); – Rajesh 2012-01-10 15:54:28