[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

XML Linq C#

Hello,

See below xml. Is it possible in linq to check if "errorCode" node exist under transactionItems-transaction if so get the errorcode and message?

Thanks
<root>
<header>
...
</header>
<transactionItems>
<transaction>
<id>123</id>
<name></name>
<errorCode>3456</errorCode>
<errorMessage>invalid id</errorMessage>
</transaction>
</transactionItems>
</root>

Open in new window

0
sansoftura
Asked:
sansoftura
  • 3
  • 2
1 Solution
 
käµfm³d 👽Commented:
How about this:
using System;
using System.Linq;
using System.Xml.Linq;

namespace _27398886
{
    class Program
    {
        static void Main(string[] args)
        {
            var errorData = from trans in XDocument.Load("input.xml").Descendants("transaction")
                            where trans.Elements("errorCode").FirstOrDefault() != null
                            select new
                            {
                                ErrorCode = trans.Element("errorCode").Value,
                                ErrorMesg = trans.Element("errorMessage").Value
                            };

            foreach (var err in errorData)
            {
                Console.WriteLine("{0}: {1}", err.ErrorCode, err.ErrorMesg);
            }

            Console.ReadKey();
        }
    }
}

Open in new window

0
 
sansofturaAuthor Commented:
Thanks the code works perfectly.

But if there is a xml namespace for the nodes <transactionItems xmlns="..."> & <transaction xmlns="..">  then it doesnt seem to work. Any idea?
0
 
käµfm³d 👽Commented:
But if there is a xml namespace for the nodes <transactionItems xmlns="..."> & <transaction xmlns="..">  then it doesnt seem to work. Any idea?
Certainly. Add the namespace as part of your search:
using System;
using System.Linq;
using System.Xml.Linq;

namespace _27398886
{
    class Program
    {
        static void Main(string[] args)
        {
            XNamespace ns = "http://namespace.example.com";
            var errorData = from trans in XDocument.Load("input.xml").Descendants(ns + "transaction")
                            where trans.Elements(ns + "errorCode").FirstOrDefault() != null
                            select new
                            {
                                ErrorCode = trans.Element(ns + "errorCode").Value,
                                ErrorMesg = trans.Element(ns + "errorMessage").Value
                            };

            foreach (var err in errorData)
            {
                Console.WriteLine("{0}: {1}", err.ErrorCode, err.ErrorMesg);
            }

            Console.ReadKey();
        }
    }
}

Open in new window

0
 
sansofturaAuthor Commented:
Thanks!
0
 
käµfm³d 👽Commented:
NP. Glad to help  = )
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now