Taryn
Taryn

Reputation: 247710

Looping through XML

I am really struggling with how to accomplish the following in my winforms C# application. I have code that works in an old Access database with VBA but I need to convert it.

  1. I have an XML file that I receive that has transaction data for a particular user.
  2. I also have a list of actions that are stored in a SQL server environment.
  3. I need to loop through the transactions in the XML to see if the action of the transaction is one of the actions in the second step.
  4. If after looping through my list of actions, I have any transactions left. Return the record count.

Sample XML:

<TransactionsResponse>
- <Transactions>
- <Transaction>
        <AccountId>1</AccountId> 
        <ActionType>test</ActionType> 
   </Transaction>
- <Transaction>
        <AccountId>1</AccountId> 
        <ActionType>blah</ActionType> 
   </Transaction>
- <Transaction>
        <AccountId>1</AccountId> 
        <ActionType>no</ActionType> 
   </Transaction>
- <Transaction>
        <AccountId>1</AccountId> 
        <ActionType>yes</ActionType> 
   </Transaction>
- <Transaction>
        <AccountId>1</AccountId> 
        <ActionType>testing</ActionType> 
   </Transaction>
- <Transaction>
        <AccountId>1</AccountId> 
        <ActionType>lame</ActionType> 
   </Transaction>
  </Transactions>
  </TransactionsResponse>

So my XML above has 6 total records, if my list of actions includes the following:

test
blah

I need to loop through the transactions and then the list of actions which would result in a final record count of 4.

I was thinking that I could first loop through the XML to get a total record count, then loop through it again, checking the action and if it exists, then drop my record count by one.

I am new to working with XML in C# so I am totally stumped on how to proceed. Any advice or suggestions would be great.

Upvotes: 2

Views: 2061

Answers (4)

dthorpe
dthorpe

Reputation: 36082

I'd suggest using Linq and XDocument to perform this work.

Load the XML into an XDocument, like var doc = XDocument.Parse(xmlText);

Load the actions you want to filter into a string list or array:

var stringlist = new List<string>() { "test", "blah" };

Use a linq query to select the transaction elements that do not that match your stringlist:

var q = from transaction in doc.Descendents("Transaction")
        where !stringlist.Contains(transaction.Element("ActionType").Value))
        select transaction;

And then count the items selected:

return q.Count();

*Note: this is off the top of my head, code may contain syntax errors.

Upvotes: 1

Aaron McIver
Aaron McIver

Reputation: 24713

You can use XDocument to parse your XML.

XDocument doc = XDocument.Parse(xml.ToString());

Once you have the XDocument instance, you can work with it to extract the information you need.

IEnumerable<XElement> transactions = doc.
                                     Descendants("TransactionsResponse").
                                     Descendants("Transactions");
foreach (XElement element in transactions)
{
     String actionType = element.Descendants("ActionType").Single().Value;
     if(myActionTypes.Contains(actionType))
           //do whatever you want since you know the action 
           //type of the transaction now
}

Upvotes: 2

Pawan Mishra
Pawan Mishra

Reputation: 7268

If you can use Linq to XML, then you can refer the following link for sample codes/examples : http://msdn.microsoft.com/en-us/vstudio/bb688087/

Upvotes: 2

Jason
Jason

Reputation: 15931

you will want to start using Linq to Xml

var xmlActions = XElement.Parse(GetXmlString());
IList<string> actionsFromDatabase = GetActionsFromDatabase();

int actionCount = xmlActions.Descendants("Transaction").Count();

foreach (var action in actionsFromDatabase)
            {
                if (xmlActions.Descendants().Any(el => el.Name == "ActionType" && el.Value == action))
                {
                    actionCount--;
                }
            }

Upvotes: 2

Related Questions