0

I have an xml document that looks like this:

<OUTPUT version="2.0">
	<RESPONSE>
		<LOAN_DATA 
			loan_id="xxxx" 
			loan_number="1111" 
			loan_type="HE" 
			status_code="OK">
		<![CDATA[<MORTGAGE_LOAN xmlns="http://www.something.com/CLF" version="1.0">
			<APPLICANTS>
				<APPLICANT 
					is_declined="N" 
					first_name="MARISOL" 
					last_name="TESTCASE" 
					m_initial="L" 
					middle_name="L" 
					ssn="000000001" >
                              </APPLICANT>
			</APPLICANTS>
                   </MORTGAGE_LOAN>]]>
		</LOAN_DATA>
	</RESPONSE>
</OUTPUT>

I can successfully read down to the CData section and can even access the CData section using the following:

string cData = "";
            XmlDocument xDoc = new XmlDocument();

            xDoc.LoadXml(loanData);
            
            XmlNode node = xDoc.DocumentElement.SelectSingleNode(@"RESPONSE/LOAN_DATA");
            XmlNode childNode = node.ChildNodes[0];
            if (childNode is XmlCDataSection)
            {
                XmlCDataSection cdataSection = childNode as XmlCDataSection;
                cData = cdataSection.Value;
            }

cData now holds:

<MORTGAGE_LOAN xmlns="http://www.something.com/CLF" version="1.0">
			<APPLICANTS>
				<APPLICANT 
					is_declined="N" 
					first_name="MARISOL" 
					last_name="TESTCASE" 
					m_initial="L" 
					middle_name="L" 
					ssn="000000001" >
                              </APPLICANT>
			</APPLICANTS>
                   </MORTGAGE_LOAN>

What I want to do is to treat this as a new xml document and read it accordingly. When using the following node is always null. I've tried several versions of the xpath and it always returns null.

XmlDocument cDataDoc = new XmlDocument();
cDataDoc.LoadXml(cData);
                                 
//node is always null here
XmlNode node
=cDataDoc.DocumentElement.SelectSingleNode("MORTGAGE_LOAN/APPLICANTS/APPLICANT");

Any help would be great. Thanks ....

Edited by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

2
Contributors
2
Replies
7
Views
7 Years
Discussion Span
Last Post by elkinsdu
0

Try out LINQ to XML. Give this sample a spin.

using System;
using System.Linq;
using System.Xml.Linq;

class Program
{
    static void Main()
    {
        string xml = @"<OUTPUT version=""2.0"">
<RESPONSE>
<LOAN_DATA 
loan_id=""xxxx"" 
loan_number=""1111"" 
loan_type=""HE"" 
status_code=""OK"">
<![CDATA[<MORTGAGE_LOAN xmlns=""http://www.something.com/CLF"" version=""1.0"">
<APPLICANTS>
<APPLICANT 
is_declined=""N"" 
first_name=""MARISOL"" 
last_name=""TESTCASE"" 
m_initial=""L"" 
middle_name=""L"" 
ssn=""000000001"" >
</APPLICANT>
</APPLICANTS>
</MORTGAGE_LOAN>]]>
</LOAN_DATA>
</RESPONSE>
</OUTPUT>";

        XDocument document = XDocument.Parse(xml);

        var query = from response in document.Descendants("RESPONSE")
                    select new
                    {
                        LoanData = new
                        {
                            LoanId = response.Element("LOAN_DATA").Attribute ("loan_id").Value,
                            LoanNumber = response.Element("LOAN_DATA").Attribute ("loan_number").Value,
                            LoanType = response.Element("LOAN_DATA").Attribute("loan_type").Value,
                            StatusCode = response.Element("LOAN_DATA").Attribute("status_code").Value
                        },
                        InnerXml = response.Element("LOAN_DATA").Value 
                    };

        foreach (var item in query)
        {
            XDocument innerDoc = XDocument.Parse(item.InnerXml);
            XNamespace ns = "http://www.something.com/CLF";

            var applicants = from applicant in innerDoc.Descendants(ns + "APPLICANT")
                             select new
                             {
                                 IsDeclined = applicant.Attribute("is_declined").Value,
                                 FirstName = applicant.Attribute("first_name").Value,
                                 LastName = applicant.Attribute("last_name").Value,
                                 MiddleInitial = applicant.Attribute("m_initial").Value,
                                 MiddleName = applicant.Attribute("middle_name").Value,
                                 SocialSecurityNumber = applicant.Attribute("ssn").Value
                             };

            foreach (var applicant in applicants)
            {
                Console.WriteLine(applicant.FirstName);
            }
        }

        Console.Read();

    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.