将项追加到XML文档中的C#问题

将项追加到XML文档中的C#问题,c#,xml,linq,appendchild,C#,Xml,Linq,Appendchild,问题 Value cannot be null. Parameter name: other 我有一个这样的XML模式,我需要将更多的产品附加到此文件,我尝试了XMLdocument和Xdocument两个类,但没有得到任何好的结果 <prodcuts> <product> <name>123</name> <price>123</price> </product> </produ

问题

Value cannot be null.
Parameter name: other
我有一个这样的XML模式,我需要将更多的产品附加到此文件,我尝试了
XMLdocument
Xdocument
两个类,但没有得到任何好的结果

<prodcuts>
  <product>
    <name>123</name>
    <price>123</price>
  </product>
</products>
例外情况

Value cannot be null.
Parameter name: other
我也在尝试
XMLdocument
课程,但没有结果


我错在哪里。。。这个问题可以通过
LINQ
或任何其他方法解决吗?

根据您的示例,到父级的路径不需要是:prodcuts/product吗

XElement parentElement = new XElement(xmldoc.XPathSelectElement("prodcuts/product"));

在您给出的示例中,它认为parentElement为null,这将在根节点下创建一个新的XElement,我认为您需要它

xmlDoc.Root.Add(
    new XElement("product",
        new XElement("name", "456"),
        new XElement("price", "456")));

您只添加了第二个XElement(“price”、“150”)……代码无法编译—您已经声明了同一个局部变量两次。请给我们真实的代码。不要
new
parentElement。找到答案也要感谢gody gray的支持!