简体   繁体   English

尝试将 XML 字符串读入 VB.NET 数据表

[英]Trying to Read an XML String Into a VB.NET DataTable

I have an XML string based on the following...我有一个基于以下内容的 XML 字符串...

<imsapi>
<status>0</status>
<statusmessage>Login OK</statusmessage>
<zoneresponse>
    <tasks>
        <task>
            <contact>
                <surname>Last Name</surname>
                <givennames>First Name</givennames>
                <userid>JScqTyVQXFwgCg==</userid>
            </contact>
            <contactname>User Name</contactname>
            <org>
                <orgid>JScqTyVRXFQgCg==</orgid>
                <orgname>Business Unit Name</orgname>
            </org>
            <requestdatetime>2019/06/26 08:56:09</requestdatetime>
            <status>Not Started</status>
            <readtaskdatetime>2019/07/04 12:44:08</readtaskdatetime>
            <description>Job description goes here...</description>
            <custon>INTERNAL</custon>
            <duedate>2020/01/13</duedate>
            <lastupdated>2020/01/06</lastupdated>
            <jobnumber>2387</jobnumber>
            <duedatetime>2020/01/13 00:00:00</duedatetime>
            <client>
                <clientid>JScqTyVRXFQgCg==</clientid>
                <clientname>Client Name</clientname>
            </client>
            <taskname>Job Name</taskname>
            <taskid>JiYqUyVRPEslCg==</taskid>
            <lastupdateddatetime>2020/01/06 10:39:44</lastupdateddatetime>
            <requestdate>2019/06/26</requestdate>
            <tasktype>Manufacturing Order</tasktype>
            <substatus>
                <substatusid>IyZaVywK</substatusid>
                <substatus>Not Started</substatus>
            </substatus>
        </task>
      </tasks>
    </zoneresponse>
</imsapi>

And I am loading the XML string into a dataset to load into a vb.net datagrid.我正在将 XML 字符串加载到数据集中以加载到 vb.net 数据网格中。 When I run my code I am getting an error Child list for field task cannot be created and I can't figure out what would be causing this and how to resolve it.当我运行我的代码时,我收到一个错误Child list for field task cannot be created ,我无法弄清楚是什么导致了这个问题以及如何解决它。

My loading code is...我的加载代码是...

dim ds as new DataSet()
ds.ReadXML(New System.IO.StringReader(xmlString))
DataGridView1.DataSource = ds
DataGridView1.DataMember = "task"

The XML is loading from the web server correctly as I can output it to a text field without any issue, it is only having an issue when I attempt to load it into the datagridview while defining that the task items are what I want to display. XML 正在从 Web 服务器正确加载,因为我可以将它输出到文本字段而没有任何问题,只有当我尝试将它加载到 datagridview 时才出现问题,同时定义任务项是我想要显示的内容。

are you missing a closing "tasks" tag?您是否缺少关闭的“任务”标签?

I'm no xml expert so I used an xml checker and got:我不是 xml 专家,所以我使用了 xml 检查器并得到:

An error has been found!发现错误! Errors in the XML document: 40: 7 The element type "tasks" must be terminated by the matching end-tag " XML 文档中的错误:40: 7 元素类型“tasks”必须由匹配的结束标记“终止

hope that helps.希望有帮助。

The XML you posted IS invalid, it is missing the ending tag for tasks .您发布的XML无效的,它缺少了任务的结束标记。 Once I fixed the XML the following code ran.一旦我修复了 XML,就会运行以下代码。 Don't know that it produces what you expect.不知道它会产生你所期望的。

    Dim xe As XElement
    'you could replace the literal assignment with
    '   xe = XElement.Load("path here")
    xe = <imsapi>
             <status>0</status>
             <statusmessage>Login OK</statusmessage>
             <zoneresponse>
                 <tasks>
                     <task>
                         <contact>
                             <surname>Last Name</surname>
                             <givennames>First Name</givennames>
                             <userid>JScqTyVQXFwgCg==</userid>
                         </contact>
                         <contactname>User Name</contactname>
                         <org>
                             <orgid>JScqTyVRXFQgCg==</orgid>
                             <orgname>Business Unit Name</orgname>
                         </org>
                         <requestdatetime>2019/06/26 08:56:09</requestdatetime>
                         <status>Not Started</status>
                         <readtaskdatetime>2019/07/04 12:44:08</readtaskdatetime>
                         <description>Job description goes here...</description>
                         <custon>INTERNAL</custon>
                         <duedate>2020/01/13</duedate>
                         <lastupdated>2020/01/06</lastupdated>
                         <jobnumber>2387</jobnumber>
                         <duedatetime>2020/01/13 00:00:00</duedatetime>
                         <client>
                             <clientid>JScqTyVRXFQgCg==</clientid>
                             <clientname>Client Name</clientname>
                         </client>
                         <taskname>Job Name</taskname>
                         <taskid>JiYqUyVRPEslCg==</taskid>
                         <lastupdateddatetime>2020/01/06 10:39:44</lastupdateddatetime>
                         <requestdate>2019/06/26</requestdate>
                         <tasktype>Manufacturing Order</tasktype>
                         <substatus>
                             <substatusid>IyZaVywK</substatusid>
                             <substatus>Not Started</substatus>
                         </substatus>
                     </task>
                 </tasks>
             </zoneresponse>
         </imsapi>

    Dim ds As New DataSet()
    ds.ReadXml(xe.CreateReader)
    DataGridView1.DataSource = ds
    DataGridView1.DataMember = "task"

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

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