2014-05-22 60 views
0

我需要2个节点。XML节点消失

<OrderItem> 
    +<Product PNR="FFQK2P" Type="Hotel"> 
    +<Product PNR="SACN8L" Type="Flight"> 
<OrderItem> 

每个产品节点都有两个相同的节点。他们是“PriceInfo”和“SearchParameters”。我得到他们;

XmlNode xmlHotelPriceInfo = orderItem.ParentNode.SelectSingleNode("OrderItem/Product/PriceInfo"); 
XmlNode xmlHotelSearchParametes = orderItem.ParentNode.SelectSingleNode("OrderItem/Product/SearchParameters"); 

所以我appent节点;

xmlHotel.AppendChild(xmlHotelPriceInfo); 
xmlHotel.AppendChild(xmlProductItemInfo); 
xmlHotel.AppendChild(xmlHotelSearchParametes); 
orderItem.AppendChild(xmlHotel); 

该代码完美地创建节点。在第一个产品中;

<Product PNR="FFQK2P" Type="Hotel"> 
    <PriceInfo> 
    <ProductItemInfo 
    <SearchParameters> 
</Product> 

该代码在for循环。在第二个周期中;

xmlFlight.AppendChild(xmlFlightPriceInfo); 
xmlFlight.AppendChild(xmlProductItemInfo); 
xmlFlight.AppendChild(xmlFlightSearchParametes); 
orderItem.AppendChild(xmlFlight); 

它附加了第二个产品。但是它从第一次删除了PriceInfo和SearchParameters。它看起来;

<Product PNR="FFQK2P" Type="Hotel"> 
    +<ProductItemInfo> 
</Product> 
<Product PNR="SACN8L" Type="Flight"> 
    +<PriceInfo> 
    +<ProductItemInfo> 
    +<SearchParameters> 
</Product> 

但我需要像;

<Product PNR="FFQK2P" Type="Hotel"> 
    +<PriceInfo> 
    +<ProductItemInfo> 
    +<SearchParameters> 
</Product> 
<Product PNR="SACN8L" Type="Flight"> 
    +<PriceInfo> 
    +<ProductItemInfo> 
    +<SearchParameters> 
</Product> 

为什么会出现这种情况?

+0

它就像你添加相同的元素,两位家长的声音给我。这是行不通的。 – svick

回答

2

通过AppendChild方法,您只需更改子节点的所有者(父)节点。 看看CloneNode method

所以,你的代码应该类似于:

xmlHotel.AppendChild(xmlHotelPriceInfo); 
xmlHotel.AppendChild(xmlProductItemInfo); 
xmlHotel.AppendChild(xmlHotelSearchParametes); 
orderItem.AppendChild(xmlHotel); 

... 

xmlFlight.AppendChild(xmlFlightPriceInfo.CloneNode(true)); 
xmlFlight.AppendChild(xmlProductItemInfo.CloneNode(true)); 
xmlFlight.AppendChild(xmlFlightSearchParametes.CloneNode(true)); 
orderItem.AppendChild(xmlFlight);