开发者

XElement.Element error

开发者 https://www.devze.com 2023-03-14 13:58 出处:网络
I get a \"Object not set to an....\" when I try to do this. The exceptions hits me on the last line. xml.Add(new XElement(\"Root\", \"\"));

I get a "Object not set to an...." when I try to do this. The exceptions hits me on the last line.

xml.Add(new XElement("Root", ""));
xml.Element("Root").Add(new XElement("Sites", ""));
xmlContent = xmlContent.E开发者_运维技巧lement("Root").Element("Sites");

Anyone ?


xmlContent is null or xmlElement doesn't contain an element named Root. That's all I can say from that little code.


The solution:

From:

xml.Add(new XElement("Root", ""));
xml.Element("Root").Add(new XElement("Sites", ""));
xmlContent = xmlContent.Element("Root").Element("Sites");

To:

xml.Add(new XElement("Root", ""));
xml.Element("Root").Add(new XElement("Sites", ""));
xmlContent = xml.Element("Root").Element("Sites");

I just needed to use the correct instance, xml and not xmlContent.

Thanks!

0

精彩评论

暂无评论...
验证码 换一张
取 消