我目前正在处理一个XML请求,并且正在尝试创建一个在调用中具有多个同名节点的回复文档,所以我试图返回是:向XML文档追加多个类似的XML节点
<Reply Document>
<ConfirmationItem name = "One">
<ItemDetail />
</ConfirmationItem>
<ConfirmationItem name = "Two">
<ItemDetail />
</ConfirmationItem>
...
<ConfirmationItem name = "Twenty">
<ItemDetail />
</ConfirmationItem>
</Reply Document>
我做了一些研究,发现这个线程:其中XmlReader AppendChild is not appending same child value接受的答案是,OP必须创建新的元素,能够追加到尾部,而不是覆盖第一个。
我原来的代码如下,它创建从传入请求的XmlNode,并将结果追加到XmlDocument的本身:
//p_transdoc is the XmlDocument that holds all the items to process.
XmlNodeList nodelst_cnfrm = p_transdoc.SelectNodes("//OrderRequest");
foreach (XmlNode node in nodelst_cnfrm)
{
//this is just an XML Object
XmlNode node_cnfrm_itm = this.CreateElement("ConfirmationItem");
node_cnfrm_itm.Attributes.Append(this.CreateAttribute("name")).InnerText = p_transdoc.Attributes["name"].InnerText;
XmlNode node_itmdtl = this.CreateElement("ItemDetail");
node_cnfrm_itm.AppendChild(node_itmdtl);
//xml_doc is the return XML request
xml_doc.AppendChild(node_cnfrm_itm);
}
所以读取线程和答案后,我试图改变代码每次使用新的XmlElement。
//p_transdoc is the XmlDocument that holds all the items to process.
XmlNodeList nodelst_cnfrm = p_transdoc.SelectNodes("//OrderRequest");
foreach (XmlNode node in nodelst_cnfrm)
{
XmlElement node_cnfrm_itm = new XmlElement();
node_cnfrm_itm = this.CreateElement("ConfirmationItem");
node_cnfrm_itm.Attributes.Append(this.CreateAttribute("name")).InnerText = p_transdoc.Attributes["name"].InnerText;
XmlElement node_itmdtl = new XmlElement();
node_itmdtl = this.CreateElement("ItemDetail");
node_cnfrm_itm.AppendChild(node_itmdtl);
//xml_doc is the return XML request
xml_doc.AppendChild(node_cnfrm_itm);
}
但不仅如此,它会返回服务器错误。所以我来找你寻求帮助。此时此代码仅返回一个ConfirmationItem。我如何能够将ConfirmationItem附加到文档的末尾而不是覆盖它,以便能够返回与发送的数量相同的数量? (我应该指出,为了便于阅读,简化和减少混乱,这些代码已经被大量格式化,任何印刷错误纯粹是因为Asker在有效校对时发生的内部故障)。
什么样的对象是 '这个'? – 2011-03-15 14:17:33