2011-03-15 60 views
4

我目前正在处理一个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在有效校对时发生的内部故障)。

+1

什么样的对象是 '这个'? – 2011-03-15 14:17:33

回答

1

假设xml_doc是带有ConfirmationItems的xml,您需要使用新的XmlDocument创建XmlElements。 XmlDocument.CreateElement。因此,我在这里使用Linq扩展方法OfType<>()仅返回XmlElement类型的XmlNode对象。

// 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; 
} 

CreateElement返回XmlElement的,所以你可以使用的方法SetAttributeGetAttribute的方法。代码:p_transdoc.Attributes["name"].InnerText似乎不正确。如果你想获得文档根元素的属性,你需要输入:p_transdoc.DocumentElement.GetAttribute("name")

如果你使用Linq to XML,IMO更容易。

LINQ to XML中,这将是类似的(某些变量有不同的名称):

// dummy data 
var transDoc = XDocument.Parse(@" 
<root name='rootAttribute'> 
    <OrderRequest name='one' /> 
    <OrderRequest name='two' /> 
    <OrderRequest name='three' /> 
</root>"); 

var xmlDoc = XDocument.Parse("<ReplyDocument />"); 

xmlDoc.Root.Add(
    transDoc.Root.Elements("OrderRequest").Select(o => 
     new XElement("ConfirmationElement", 
      new XAttribute("name", (string)o.Attribute("name")), 
      new XElement("ItemDetail")))); 

两个例子输出:

<ReplyDocument> 
    <ConfirmationElement name="one"> 
    <ItemDetail /> 
    </ConfirmationElement> 
    <ConfirmationElement name="two"> 
    <ItemDetail /> 
    </ConfirmationElement> 
    <ConfirmationElement name="three"> 
    <ItemDetail /> 
    </ConfirmationElement> 
</ReplyDocument> 
+0

非常感谢你的回复。我将不得不对Linq to XML做更多的研究,以充分理解第二个例子。第一个例子,虽然我有问题,也许这是因为我缺乏经验。但是,我没有看到你自己附加元素的位置。这是系统自动执行的事情吗? – Gobbledigook 2011-03-15 16:01:19

+0

此外,只是想指出:p_transdoc.Attributes [“name”]。InnerText绝对不是最优雅的解决方案(我知道现在!:)),但它是可行的,至少在foreach的上下文中循环本身。它从OrderRequest节点本身获取名称属性,因为这是从p_transdoc预加载的,也就是说。的SelectNodes( “// OrderRequest”);只想给你提供信息的目的:) – Gobbledigook 2011-03-15 16:09:26

+0

我创建了节点,并将其附加到单行中的文档中:xml_doc.DocumentElement.AppendChild(xml_doc.CreateElement(“ConfirmationItem”));.为了提高可读性,您可以更改为:var n = xml_doc.CreateElement(“ConfirmationItem”); n = xml_doc.DocumentElement.AppendChild(n); – 2011-03-15 16:43:29