Click here to Skip to main content
15,906,463 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello experts, i want to extract the an XML element are their corresponding value in to a List of NameValueCollection like (List<namevaluecollection>), and also retrieve all the CDATA present in the element into the List as well using LINQ to XML code in C#. and also i want to allow user to just enter an Element name in the method below a the method should display its value or its CDATA value. Thanks

Here the XML Data.

XML
<?xml version=\"1.0\" encoding=\"utf-8\" ?>
<TRANS_NET>
<REQUEST_ID>1278999</REQUEST_ID>
<REQUEST_DATE>2013-12-06 12:10:23.0</REQUEST_DATE>

<RESPONSE_CODE>001</RESPONSE_CODE>
<RESPONSE_REASON>
<SUCCESS>100</SUCCESS>
</RESPONSE_REASON>

<CATALOG>
<CARDDATA>
<SERIAL_NO>11177202</SERIAL_NO>
<BATCH_NO>3980</BATCH_NO>
<BASE_CARD_TYPE> <![CDATA[REV </BASE_CARD_TYPE>
<CARD_TYPE><![CDATA[PRDC</CARD_TYPE>
<CARD_NAME><![CDATA[inCharge Global Cards</CARD_NAME>
<CARD_STATUS>A</CARD_STATUS>
<CURRENCY_CODE><![CDATA[GHS</CURRENCY_CODE>
<CURRENT_BALANCE>0.000</CURRENT_BALANCE>
<EXPIRED><![CDATA[N</EXPIRED>
<MAX_CAP>100000000000.000</MAX_CAP>
<EXP_DATE><![CDATA[2016-10</EXP_DATE>
</CARDDATA>
</CATALOG>
</TRANS_NET>

An here is my Lin


Here is my C# Code
public NameValueCollection GenericGetXMLTreeValue(string xmlTag, string toTraverse)
        {
            if (xmlTag == null || xmlTag.Length <= 0)
                throw new ArgumentNullException("Element To Check Can Not Be Empty");
            if (toTraverse == null || toTraverse.Length <= 0)
                throw new ArgumentNullException("XML Response Can Not Be Empty");

            NameValueCollection arrXMLResponse = new NameValueCollection();

            XElement cardData = XElement.Parse(toTraverse);
            foreach (XElement eachXelement in cardData.Descendants())
            {
                arrXMLResponse.Add(eachXelement.Element(xmlTag).Name.LocalName, eachXelement.Element(xmlTag).Value);
            }

            return arrXMLResponse;
        }</namevaluecollection>
Posted
Updated 22-Dec-13 5:30am
v3

 
Share this answer
 
If logical XML structure, there is no such thing as CDATA, this is no more than a way to "escape" all XML formatting characters in some string value, a part of physical XML structure. In other workds, "CDATA" is not a part of the data structure stored in XML, it is yet another artifact of XML formatting. You simply need to get the text node of your XML element BASE_CARD_TYPE.

If you want to use LINQ to XML programming, the most adequate XML parser would be System.Xml.Linq.XDocument, read the MSDN documentation on it: http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
v2
Comments
ahmedfaruk88 23-Dec-13 7:54am    
Thank you very much. i am surprise how you guys know so much. Thanks to codeproject too. I will check it out and get back to you. Please can i always contact you when i have a challenge.
Sergey Alexandrovich Kryukov 23-Dec-13 12:56pm    
You are welcome. Please don't forget to accept the answer formally (green "Accept" button) if you find it makes sense for you, but your follow-up questions will be welcome.
If you want to ask a question and bring it to my attention, you can add a comment to any of my existing posts, like this one. I receive notifications on all comments to my posts and other relevant posts.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900