简体   繁体   English

解码XML字符串

[英]Decoding XML String

I am having trouble decoding some encoded xml. 我在解码某些编码的xml时遇到问题。

I am getting "XmlException Unhandled: Data at the root level is invalid. Line 1 Position 1" 我收到“未处理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: &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;&#xA;&lt;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'&gt;&#xA;  &lt;lbct:create-truck-visit-response&gt;&amp;lt;![CDATA[&amp;lt;argo:gate-response xmlns:argo=&quot;(Had to remove link)&quot; xmlns:xsi=&quot;(Had to remove link)&quot; xsi:schemaLocation=&quot(Had to remove link)&quot;&amp;gt;&amp;lt;create-truck-visit-response&amp;gt;&amp;lt;truck-visit tv-key=&quot;52947590&quot; gos-tv-key=&quot;123&quot; tracking-tag-id=&quot;98765432&quot; is-internal=&quot;false&quot; next-stage-id=&quot;ingate&quot; status=&quot;OK&quot; gate-id=&quot;LBCT&quot; entered=&quot;2014-01-23T05:47:28&quot;&amp;gt;&amp;lt;trucking-co id=&quot;WNWI&quot; /&amp;gt;&amp;lt;truck id=&quot;98765432&quot; tag-id=&quot;98765432&quot; license-nbr=&quot;D98765432&quot; /&amp;gt;&amp;lt;chassis-profile id=&quot;40&quot; /&amp;gt;&amp;lt;driver card-id=&quot;98765432&quot; driver-name=&quot;LICENSEABC&quot; /&amp;gt;&amp;lt;/truck-visit&amp;gt;&amp;lt;/create-truck-visit-response&amp;gt;&amp;lt;/argo:gate-response&amp;gt;]]&amp;gt;&lt;/lbct:create-truck-visit-response&gt;&#xA;  &lt;lbct:process-truck-response&gt;&amp;lt;![CDATA[Unknown IN lane LANE20 for gate LBCT&amp;lt;argo:gate-response xmlns:argo=&quot;(Had to remove link)&quot; xmlns:xsi=&quot;(Had to remove link)&quot; xsi:schemaLocation=&quot;(Had to remove link)&quot;&amp;gt;&amp;lt;process-truck-response /&amp;gt;&amp;lt;/argo:gate-response&amp;gt;]]&amp;gt;&lt;/lbct:process-truck-response&gt;&#xA;&lt;/lbct:submit-ingate-data-response&gt;" message-detail="Result: &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;&#xA;&lt;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'&gt;&#xA;  &lt;lbct:create-truck-visit-response&gt;&amp;lt;![CDATA[&amp;lt;argo:gate-response xmlns:argo=&quot;(Had to remove link)&quot; xmlns:xsi=&quot;(Had to remove link)&quot; xsi:schemaLocation=&quot;(Had to remove link)&quot;&amp;gt;&amp;lt;create-truck-visit-response&amp;gt;&amp;lt;truck-visit tv-key=&quot;52947590&quot; gos-tv-key=&quot;123&quot; tracking-tag-id=&quot;98765432&quot; is-internal=&quot;false&quot; next-stage-id=&quot;ingate&quot; status=&quot;OK&quot; gate-id=&quot;LBCT&quot; entered=&quot;2014-01-23T05:47:28&quot;&amp;gt;&amp;lt;trucking-co id=&quot;WNWI&quot; /&amp;gt;&amp;lt;truck id=&quot;98765432&quot; tag-id=&quot;98765432&quot; license-nbr=&quot;D98765432&quot; /&amp;gt;&amp;lt;chassis-profile id=&quot;40&quot; /&amp;gt;&amp;lt;driver card-id=&quot;98765432&quot; driver-name=&quot;LICENSEABC&quot; /&amp;gt;&amp;lt;/truck-visit&amp;gt;&amp;lt;/create-truck-visit-response&amp;gt;&amp;lt;/argo:gate-response&amp;gt;]]&amp;gt;&lt;/lbct:create-truck-visit-response&gt;&#xA;  &lt;lbct:process-truck-response&gt;&amp;lt;![CDATA[Unknown IN lane LANE20 for gate LBCT&amp;lt;argo:gate-response xmlns:argo=&quot;(Had to remove link)&quot; xmlns:xsi=&quot;(Had to remove link)&quot; xsi:schemaLocation=&quot;(Had to remove link)&quot;&amp;gt;&amp;lt;process-truck-response /&amp;gt;&amp;lt;/argo:gate-response&amp;gt;]]&amp;gt;&lt;/lbct:process-truck-response&gt;&#xA;&lt;/lbct:submit-ingate-data-response&gt;" />
  </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>&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;</create-truck-visit-response>
    <process-truck-response>&lt;![CDATA[Unknown IN lane LANE20 for gate LBCT&lt;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"&gt;&lt;process-truck-response /&gt;&lt;/argo:gate-response&gt;]]&gt;</process-truck-response>
  </submit-ingate-data-response>
</argo-response>

The only portion I require is the process-truck-response. 我唯一需要的部分是过程卡车响应。 I have been able to successfully pull the create-truck-visit out of it. 我已经能够成功撤出create-truck-visit。

Here is the code i am currently using to try and decode and parse this: 这是我当前用来尝试解码和解析的代码:

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
                {

                }
            }

Any help would be much appreciated. 任何帮助将非常感激。

When XML tools say "invalid" (as opposed to "not well-formed") they generally mean that you've handed them the wrong kind of document. 当XML工具说“无效”(而不是“格式不正确”)时,它们通常意味着您向他们提交了错误的文档。 Check what this tool expects. 检查此工具的期望。 In particular, since you haven't specified namespaces in the sample you've shown us, check whether it expects namespace bindings. 特别是,由于您没有在示例中指定名称空间,因此请检查是否需要名称空间绑定。

The contents of the process-truck-response element doesn't consist of a valid XML fragment. 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>

You're attempting to parse that out as an XElement and it fails (and rightly so). 您试图将其解析为XElement ,但失败了(正确的做法是)。

You should verify that you have valid XML or at least handle cases where it contains content that you're not expecting. 您应该验证自己是否具有有效的XML,或者至少要处理包含不期望的内容的XML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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