我试图将带有CData的xml字符串放在元素中。但解析部分XDocument.Parse在解析CDATA时发生"文件意外结束失败"。我显然可以清理xml字符串并删除CData,但为什么不解析?
//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();
}
答案 0 :(得分:1)
CDATA
错过了结束>
。这是正确的语法:
<![CDATA[topsecret]]>