简体   繁体   中英

XDocument.Parse throws an exception with CData inside XML

I'm trying to put the xml string with a CData inside an element. But the parsing part XDocument.Parse fails with "Unexpected end of file while parsing CDATA has occurred". I can obviously cleanse the xml string and remove the CData, but why isn't this parsing?

//this throws an exception
var xDoc = XDocument.Parse(GetSampleXmlText());

private string GetSampleXmlText()
    {
        //WARNING: Tests will break if element order/hierarchy is changed

        var xml =
            @"<?xml version=""1.0"" encoding=""UTF-8""?>                                                 " +
            @"<ac_application>                                                                           " +
            @"    <primary_applicant_data>                                                               " +
            @"     <first_name>Joe</first_name>                                                          " +
            @"     <middle_name></middle_name>                                                           " +
            @"     <last_name>Irving</last_name>                                                         " +
            @"     <ssn>123456789</ssn>                                                                  " +
            @"     <dob>09/09/1979</dob>                                                                 " +
            @"     <email_address>test123@gmail.com</email_address>                                      " +
            @"     <home_phone>3215551212</home_phone>                                                   " +
            @"     <mobile_phone>3215898888</mobile_phone>                                               " +
            @"     <contact_number officePhone=""1x34ad789"" />                                          " +
            @"     <contact_number cellPhone=""(321)898-8888"" />                                        " +
            @"     <other_phone>(321)666-7777</other_phone>                                              " +
            @"     <invalid_phone>32aZ9xd8</invalid_phone>                                               " +
            @"     <valid_id type=""SSN"">9999999990</valid_id>                                          " +
            @"     <valid_id type=""ITIN"">99-999-99990</valid_id>                                       " +
            @"     <address type=""current"">                                                            " +
            @"        <street_no>14</street_no>                                                          " +
            @"        <street_name>B</street_name>                                                       " +
            @"        <apt_no>155</apt_no>                                                               " +
            @"        <city>Santa Ana</city>                                                             " +
            @"        <state>CA</state>                                                                  " +
            @"        <zip_code>6654</zip_code>                                                          " +
            @"     </address>                                                                            " +
            @"     <address type=""permanent"">                                                          " +
            @"        <street_no>54</street_no>                                                          " +
            @"        <street_name>B</street_name>                                                       " +
            @"        <apt_no>104</apt_no>                                                               " +
            @"        <city>Santa Fe</city>                                                              " +
            @"        <state>CA</state>                                                                  " +
            @"        <zip_code>60750</zip_code>                                                         " +
            @"     </address>                                                                            " +
            @"    </primary_applicant_data>                                                              " +
            @"    <vehicle>                                                                              " +
            @"        <year>2013</year>                                                                  " +
            @"        <make>Toyota</make>                                                                " +
            @"        <model>Corolla</model>                                                             " +
            @"        <trim>4D Sedan</trim>                                                              " +
            @"        <odometer>175,000</odometer>                                                       " +
            @"        <price>$15,700</price>                                                             " +
            @"    </vehicle>                                                                             " +
            @"    <secret_data><![CDATA[topsecret]]</secret_data>                                        " +
            @"</ac_application>                                                                           ";

        return xml.Trim();
    }

The CDATA is missing the closing > . This is the right syntax:

<![CDATA[topsecret]]>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM