我无法解码某些编码的xml。
我收到“XmlException未处理:根级别的数据无效。第1行位置1”
<argo-response status="1" status-id="INFO">
<messages>
<message message-id="INFO" message-severity="INFO" message-text="[class-name:LBCTSubmitInGateDataWebService]" message-detail="[class-name:LBCTSubmitInGateDataWebService]" />
<message message-id="GROOVY_RESULT" message-severity="INFO" message-text="Result: <?xml version="1.0" encoding="UTF-8" ?>
<lbct:submit-ingate-data-response xmlns='(Had to remove link)' xmlns:xsi='(Had to remove link)' xmlns:lbct='(Had to remove link)' lbct:message-id='789637546382669' lbct:message-date='2014-01-23T06:37:32.0Z' lbct:facility-id='LBCT' lbct:complex-id='LBCT' lbct:sender='TOS'>
 <lbct:create-truck-visit-response>&lt;![CDATA[&lt;argo:gate-response xmlns:argo="(Had to remove link)" xmlns:xsi="(Had to remove link)" xsi:schemaLocation="(Had to remove link)"&gt;&lt;create-truck-visit-response&gt;&lt;truck-visit tv-key="52947590" gos-tv-key="123" tracking-tag-id="98765432" is-internal="false" next-stage-id="ingate" status="OK" gate-id="LBCT" entered="2014-01-23T05:47:28"&gt;&lt;trucking-co id="WNWI" /&gt;&lt;truck id="98765432" tag-id="98765432" license-nbr="D98765432" /&gt;&lt;chassis-profile id="40" /&gt;&lt;driver card-id="98765432" driver-name="LICENSEABC" /&gt;&lt;/truck-visit&gt;&lt;/create-truck-visit-response&gt;&lt;/argo:gate-response&gt;]]&gt;</lbct:create-truck-visit-response>
 <lbct:process-truck-response>&lt;![CDATA[Unknown IN lane LANE20 for gate LBCT&lt;argo:gate-response xmlns:argo="(Had to remove link)" xmlns:xsi="(Had to remove link)" xsi:schemaLocation="(Had to remove link)"&gt;&lt;process-truck-response /&gt;&lt;/argo:gate-response&gt;]]&gt;</lbct:process-truck-response>
</lbct:submit-ingate-data-response>" message-detail="Result: <?xml version="1.0" encoding="UTF-8" ?>
<lbct:submit-ingate-data-response xmlns='(Had to remove link)' xmlns:xsi='(Had to remove link)' xmlns:lbct='(Had to remove link)' lbct:message-id='789637546382669' lbct:message-date='2014-01-23T06:37:32.0Z' lbct:facility-id='LBCT' lbct:complex-id='LBCT' lbct:sender='TOS'>
 <lbct:create-truck-visit-response>&lt;![CDATA[&lt;argo:gate-response xmlns:argo="(Had to remove link)" xmlns:xsi="(Had to remove link)" xsi:schemaLocation="(Had to remove link)"&gt;&lt;create-truck-visit-response&gt;&lt;truck-visit tv-key="52947590" gos-tv-key="123" tracking-tag-id="98765432" is-internal="false" next-stage-id="ingate" status="OK" gate-id="LBCT" entered="2014-01-23T05:47:28"&gt;&lt;trucking-co id="WNWI" /&gt;&lt;truck id="98765432" tag-id="98765432" license-nbr="D98765432" /&gt;&lt;chassis-profile id="40" /&gt;&lt;driver card-id="98765432" driver-name="LICENSEABC" /&gt;&lt;/truck-visit&gt;&lt;/create-truck-visit-response&gt;&lt;/argo:gate-response&gt;]]&gt;</lbct:create-truck-visit-response>
 <lbct:process-truck-response>&lt;![CDATA[Unknown IN lane LANE20 for gate LBCT&lt;argo:gate-response xmlns:argo="(Had to remove link)" xmlns:xsi="(Had to remove link)" xsi:schemaLocation="(Had to remove link)"&gt;&lt;process-truck-response /&gt;&lt;/argo:gate-response&gt;]]&gt;</lbct:process-truck-response>
</lbct:submit-ingate-data-response>" />
</messages>
<submit-ingate-data-response xmlns:lbct="(Had to remove link)" xmlns="(Had to remove link)" xmlns:xsi="(Had to remove link)" lbct:message-id="789637546382669" lbct:message-date="2014-01-23T06:37:32.0Z" lbct:facility-id="LBCT" lbct:complex-id="LBCT" lbct:sender="TOS">
<create-truck-visit-response><![CDATA[<argo:gate-response xmlns:argo="(Had to remove link)" xmlns:xsi="(Had to remove link)" xsi:schemaLocation="(Had to remove link)"><create-truck-visit-response><truck-visit tv-key="52947590" gos-tv-key="123" tracking-tag-id="98765432" is-internal="false" next-stage-id="ingate" status="OK" gate-id="LBCT" entered="2014-01-23T05:47:28"><trucking-co id="WNWI" /><truck id="98765432" tag-id="98765432" license-nbr="D98765432" /><chassis-profile id="40" /><driver card-id="98765432" driver-name="LICENSEABC" /></truck-visit></create-truck-visit-response></argo:gate-response>]]></create-truck-visit-response>
<process-truck-response><![CDATA[Unknown IN lane LANE20 for gate LBCT<argo:gate-response xmlns:argo="http://www.navis.com/argo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.navis.com/argo GateWebserviceResponse.xsd"><process-truck-response /></argo:gate-response>]]></process-truck-response>
</submit-ingate-data-response>
</argo-response>
我需要的唯一部分是过程卡车响应。我已经成功地将创造卡车访问拉出了它。
以下是我目前用于尝试解码和解析此代码的代码:
string xmlString = textBox1.Text;
string action = String.Empty;
XDocument xmlDoc = XDocument.Parse(xmlString);
XNamespace lbctns = "http://www.lbct.com/TOS";
XElement processTruckElement = xmlDoc.Document.Descendants(lbctns + "process-truck-response").FirstOrDefault();
if (processTruckElement != null)
{
var decodedProccessTruckResponseElement = processTruckElement.Value;
if (decodedProccessTruckResponseElement != null)
{
decodedProccessTruckResponseElement = decodedProccessTruckResponseElement.Replace("<![CDATA[", "").Replace("]]>", "");
}
try
{
var parsedSubmitMultipleElement = XElement.Parse(decodedProccessTruckResponseElement);
}
catch
{
}
}
非常感谢任何帮助。
答案 0 :(得分:1)
当XML工具说“无效”(而不是“格式不正确”)时,它们通常意味着你向他们提交了错误的文档。检查此工具的期望。特别是,由于您没有在示例中指定名称空间,请检查它是否需要命名空间绑定。
答案 1 :(得分:1)
process-truck-response
元素的内容不包含有效的XML片段。
Unknown IN lane LANE20 for gate LBCT<argo:gate-response xmlns:argo="http://www.navis.com/argo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.navis.com/argo GateWebserviceResponse.xsd"><process-truck-response /></argo:gate-response>
您试图将其解析为XElement
并且失败(并且正确)。
您应该验证您是否拥有有效的XML,或至少处理包含您不期望的内容的案例。