将XmlNodeType :: Attribute的节点附加到XmlNode时出错

时间:2015-03-13 10:10:31

标签: xml dom attributes appendchild xmlnode

有人可以解释我在这里做错了我得到了这个错误:

The specified node cannot be inserted as the valid child of this node, because the specified node is the wrong type.

在此代码的最后一行?

XmlDocument^ xmlDoc = gcnew XmlDocument();
XmlNode^ xmlNode = xmlDoc->CreateNode(XmlNodeType::Element, "QualifyingProperties", "http://uri.etsi.org/01903/v1.3.2#");
XmlNode^ nodAttribute = xmlDoc->CreateNode(XmlNodeType::Attribute, "Target", "http://namespace.123");

xmlNode->AppendChild(nodAttribute);

将XmlNodeType :: Attribute类型的节点添加到XmlNode的正确方法是什么? 我知道我可以创建一个XmlAttribute并将其添加到属性中,但我想知道我尝试这样做的方式有什么问题。

1 个答案:

答案 0 :(得分:0)

这是因为,根据定义,属性不是其他节点的子节点,因此您无法将它们传递给AppendChild

他们有一个特别的地方:

xmlNode->Attributes->SetNamedItem(nodAttribute);