• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • 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
Abirami Rajendran
Asked:
Abirami Rajendran
  • 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
 
Abirami RajendranAuthor 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
 
Abirami RajendranAuthor Commented:
Thanks!
0
 
käµfm³d 👽Commented:
NP. Glad to help  = )
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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