rAm rAm - 16 days ago 3x
C# Question

Get Child Node value using parent Node in XML

How to Get the Value of the node by using the parent tag name.

Here is my XML format.

<ListOrderItemsResponse xmlns="https://mws.amazonservices.com/Orders/2013-09-01">
<AmazonOrderId>Order Id</AmazonOrderId>
<ASIN>Asin Value</ASIN>
<OrderItemId>SKU Value</OrderItemId>
<Title>Product Title</Title>

How to get Item Price Amount and Shipping Price Amount.

Here i tried so far..

Method 1:

XmlNode node12 = xd1.SelectSingleNode("/ListOrderItemsResponse[@*]/ListOrderItemsResult/OrderItems/OrderItem/ItemPrice");
string id = node12["Amount"].InnerText;

Method 2:

int i = 0;
XmlNodeList nodeAMT = xd1.GetElementsByTagName("Amount");
string[] AMT = new string[TotalCount];
foreach (XmlElement node in nodeAMT)
AMT[i] = node.InnerText;

How to Get ItemPrice 30 and ShippingPrice 5.

Any Suggestions??


This is a classical problem of default namespace. Your XML has default namespace declared at the root element :


All elements without prefix are considered in the above mentioned default namespace. To select element in namespace, you need to use XmlNamespaceManager:

var nsmgr = new XmlNamespaceManager(new NameTable());
//register mapping of prefix to namespace uri 
nsmgr.AddNamespace("d", "https://mws.amazonservices.com/Orders/2013-09-01");

string query = "/d:ListOrderItemsResponse/d:ListOrderItemsResult/d:OrderItems/d:OrderItem/d:ItemPrice/d:Amount";
XmlNode node = xd1.SelectSingleNode(query, nsmgr);
string itemPrice = node.InnerText;