将XML读入DataTable

时间:2016-03-03 19:28:05

标签: c# xml datatable dataset

我为WebRequest返回了以下XML:

<DataSet>
    <xs:schema id="FoxProDataTable" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
        <xs:element name="FoxProDataTable" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
            <xs:complexType>
                <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:element name="FoxProDataRow">
                        <xs:complexType>
                            <xs:sequence>

                                <xs:element name="d_alias" minOccurs="0">
                                    <xs:simpleType>
                                        <xs:restriction base="xs:string">
                                            <xs:maxLength value="8" />
                                        </xs:restriction>
                                    </xs:simpleType>
                                </xs:element>

                                <xs:element name="d_audit" type="xs:boolean" minOccurs="0" />

                                <xs:element name="d_auditkey" minOccurs="0">
                                    <xs:simpleType>
                                        <xs:restriction base="xs:string">
                                            <xs:maxLength value="50" />
                                        </xs:restriction>
                                    </xs:simpleType>
                                </xs:element>
                                . . .
                                . . .
                                <xs:element name="d_version" type="xs:decimal" minOccurs="0" />
                                <xs:element name="d_custom" type="xs:boolean" minOccurs="0" />
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                </xs:choice>
            </xs:complexType>
        </xs:element>
    </xs:schema>
    <diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
        <FoxProDataTable>
            <FoxProDataRow diffgr:id="FoxProDataRow1" msdata:rowOrder="0">
                <d_alias>ADJ     </d_alias>
                <d_audit>false</d_audit>
                <d_auditkey xml:space="preserve">                                                  </d_auditkey>
                <d_auditon>false</d_auditon>
                <d_chadate xml:space="preserve">          </d_chadate>
                <d_convert xml:space="preserve">                                                            </d_convert>
                <d_create>ADJ     </d_create>
                <d_desc>Employer Quarter Adjustment                       </d_desc>
                <d_encrypt>true</d_encrypt>
                <d_file>PRQTRADJ</d_file>
                <d_key1>Company   </d_key1>
                <d_key2 xml:space="preserve">          </d_key2>
                <d_key3 xml:space="preserve">          </d_key3>
                <d_key4 xml:space="preserve">          </d_key4>
                <d_massup>false</d_massup>
                <d_msc>false</d_msc>
                <d_parent xml:space="preserve">          </d_parent>
                <d_prod>PR</d_prod>
                <d_recsize>0</d_recsize>
                <d_required>true</d_required>
                <d_type>R </d_type>
                <d_version>9.100100</d_version>
                <d_custom>false</d_custom>
            </FoxProDataRow>
        </FoxProDataTable>
    </diffgr:diffgram>
</DataSet>

我试图将其读入DataSet / DataTable,如下所示:

XmlDocument _xmlDoc = GetResponseAsXml(_url, _request, HttpMethods.GET);
DataSet _dataSet = new DataSet();
_dataSet.ReadXml(new XmlTextReader(new StringReader(_xmlDoc.OuterXml)));
DataTable _dataTable = _dataSet.Tables[0];

当我检查_dataTable时,列与架构匹配:

enter image description here

但Row有以下内容:

enter image description here

如何将数据放入表中?

1 个答案:

答案 0 :(得分:0)

我使用保存在文件中的XML快速测试了这个....将XML加载到DataSet中并将其连接到dataGridView以查看您的数据.....

private void setupViewPager(ViewPager viewPager) {
    TabsAdapter adapter = new TabsAdapter(getSupportFragmentManager());
    adapter.addFrag(new BlankFragment1(), "One");
    adapter.addFrag(new BlankFragment2(), "Two");
    adapter.addFrag(new BlankFragment3(), "Three");
    viewPager.setAdapter(adapter);
}

这样工作正常,dataGridView中的所有数据......希望有帮助..