If your XML is correctly closed (i assume that the Transaction
and StoreCenter
nodes from the example XML are closed), then - yes, your approach using LINQ2XML is correct.
You could enhance the code using the XElement.GetDefaultNamespace() method to retrieve the default namespace easily (easier than typing). A solution could look like this:
var xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<StoreCenter Operation=""update"" xmlns=""http://something.com/rdc.xsd"">
<Transaction>
<Transaction>
<StoreID>30</StoreID>
<TransactionID>2</TransactionID>
<RegisterTransNumber>2</RegisterTransNumber>
</Transaction>
<Transaction>
<StoreID>30</StoreID>
<TransactionID>3</TransactionID>
<RegisterTransNumber>2</RegisterTransNumber>
</Transaction>
</Transaction>
</StoreCenter>";
var xmlDocument = XDocument.Parse(xml); // or XDocument.Load(xml);
var ns = xmlDocument.Root.GetDefaultNamespace();
var transactions = xmlDocument
.Root // the StoreCenter root node
.Element(ns + "Transaction") // the enclosing Transaction node
.Elements(ns + "Transaction") // only Transaction subnodes
.ToList();
foreach (var transaction in transactions)
{
var storeId = transaction.Element(ns + "StoreID").Value;
Console.WriteLine(storeId);
}
And the output is:
30
30