C# 将多个类似的XML节点追加到XML文档
我目前正在处理一个XML请求,并尝试创建一个应答文档,该文档在调用中包含多个同名子节点,因此我尝试返回的是:C# 将多个类似的XML节点追加到XML文档,c#,xml,C#,Xml,我目前正在处理一个XML请求,并尝试创建一个应答文档,该文档在调用中包含多个同名子节点,因此我尝试返回的是: <Reply Document> <ConfirmationItem name = "One"> <ItemDetail /> </ConfirmationItem> <ConfirmationItem name = "Two"> <ItemDetail />
<Reply Document>
<ConfirmationItem name = "One">
<ItemDetail />
</ConfirmationItem>
<ConfirmationItem name = "Two">
<ItemDetail />
</ConfirmationItem>
...
<ConfirmationItem name = "Twenty">
<ItemDetail />
</ConfirmationItem>
</Reply Document>
//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);
}
因此,在阅读了该线程和答案之后,我尝试更改代码,以便在每次通过时使用新的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);
}
但这不仅不起作用,还返回一个服务器错误。所以我来找你帮忙。现在这段代码只返回一个确认项。我如何才能将确认项附加到文档末尾,而不是覆盖它,以便能够返回发送的数量
//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);
}
(我应该指出,为了便于阅读、简单和减少混乱,这段代码的格式非常严格。任何印刷错误都纯粹是因为提问者在有效校对方面的内部失误) 假设xml_doc是带有确认项的xml,则需要使用新的XmlDocument创建XmlElements
XmlDocument.CreateElement
。因此,我在这里使用类型()的Linq扩展方法OfType()
,只返回XmlElement类型的XmlNode对象
//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);
}
// dummy data
XmlDocument p_transdoc = new XmlDocument();
p_transdoc.LoadXml(@"
<root name='rootAttribute'>
<OrderRequest name='one' />
<OrderRequest name='two' />
<OrderRequest name='three' />
</root>
");
XmlDocument xml_doc = new XmlDocument();
xml_doc.LoadXml("<ReplyDocument />");
foreach (var node in p_transdoc.SelectNodes("//OrderRequest").OfType<XmlElement>())
{
XmlElement node_cnfrm_itm = xml_doc.CreateElement("ConfirmationItem");
node_cnfrm_itm = xml_doc.DocumentElement.AppendChild(node_cnfrm_itm) as XmlElement;
node_cnfrm_itm.SetAttribute("name", node.GetAttribute("name"));
XmlElement node_itmdtl = xml_doc.CreateElement("ItemDetail");
node_itmdtl = node_cnfrm_itm.AppendChild(node_itmdtl) as 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);
}
<ReplyDocument>
<ConfirmationElement name="one">
<ItemDetail />
</ConfirmationElement>
<ConfirmationElement name="two">
<ItemDetail />
</ConfirmationElement>
<ConfirmationElement name="three">
<ItemDetail />
</ConfirmationElement>
</ReplyDocument>
this是什么类型的对象?感谢您明确的回答。为了完全理解第二个示例,我必须对Linq to XML做更多的研究。第一个例子,虽然我有问题,这可能是因为我的经验不足。但是,我看不到您在哪里添加元素本身。这是系统自动完成的吗?另外,我只想指出:p_transdoc.Attributes[“name”]。InnerText绝对不是最优雅的解决方案(我现在知道了!:),但它是可行的,至少在foreach循环本身的上下文中是可行的。它从OrderRequest节点本身获取name属性,因为这是从p_transdoc.SelectNodes(“//OrderRequest”)中预加载的属性;我只是想为您提供一些信息:)我创建了节点,并将其附加到文档的一行中:xml_doc.DocumentElement.AppendChild(xml_doc.CreateElement(“ConfirmationItem”);。为了提高可读性,您可以更改为:var n=xml_doc.CreateElement(“确认项”);n=xml_doc.DocumentElement.AppendChild(n)@Gobbledigook我更改了第一个代码示例,并分隔了创建元素并将其附加到文档中的行。