Question

Hi have the following XML with the same Transaction name (can't change it because this is how it comes from the origin):

<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>

I have the following code and I'm new in LINQ, I'm trying to retrive the StoreID for each Transaction Child:

XDocument Doc = XDocument.Load(filename);
       XNamespace ns = "http://something.com/rdc.xsd";
        foreach (var StoreCenter in Doc.Descendants(ns + "StoreCenter"))
        {
            foreach (var Transaction in StoreCenter.Descendants("Transaction"))
            {
                 foreach (var TransactionCh in Transaction.Descendants("Transaction"))
                 {
                    Console.WriteLine(Transaction.Element("StoreID").Value);
                 }

            }
        }

But I'm getting nothing, what am I doing wrong?, is this a good approach to retrieve these values?, please your advice will be appreciated

Était-ce utile?

La solution 2

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

Autres conseils

Remove the curly brace from here:

Doc.Descendants("{" + ns + "StoreCenter")
                ^^^

And you should specify the namespace with child elements as well.Like: StoreCenter.Descendants(ns + "Transaction") and Transaction.Descendants(ns + "Transaction")

if you just want to get Transaction elements you can just to : Doc.Descendants(ns + "Transaction") or Doc.Root.Element(ns + "Transaction").Elements(ns + "Transaction"); (assuming StoreCenter is the root element)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top