使用LINQ插入XML元素時出現問題。這是我的計劃:使用LINQ插入XML元素
XDocument doc;
protected void CreateXml()
{
doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("Sample RSS Feed"),
new XElement("rss",
new XAttribute("version", "2.0"),
new XElement("channel",
new XElement("title", "aaa"),
new XElement("description", "bbb"),
new XElement("link", "http://abcd.com"),
new XElement("language", "en"))
)
);
}
protected void HandlingData()
{
//...
EditXml();
}
protected void EditXml()
{
doc.Element("rss").Element("chanel")
.Element("language").AddAfterSelf(
new XElement("item", new XElement("title", "ccc"),
new XElement("link","..."),
new XElement("pubDate",
DateTime.Now.ToUniversalTime())));
}
逮住錯誤:的NullReferenceException未處理在EditXml()函數。你們能幫我解決嗎?非常感謝! :)