将xml反序列化为数据集/数据表

时间:2015-08-10 09:22:42

标签: xml vb.net serialization

我有一个客户端/服务器程序通过http(监听器和网络服务器)进行通信

服务器从数据库读取并将数据发送回客户端。

在服务器中,其中一个函数读取定义为:

的数据表
SELECT computertable.Name, computerprinter.`Default`, printer.Name AS printerName, printer.`Connection` FROM computerprinter INNER JOIN computertable ON computerprinter.Computer = computertable.ID INNER JOIN printer ON computerprinter.Printer = printer.ID WHERE (computertable.Name = @myComputer)

将以下XML作为内存流返回给客户端。

<?xml version="1.0"?>
<myPrintersDataTable>
  <xs:schema id="gatekeeperdbDataSet" targetNamespace="http://tempuri.org/gatekeeperdbDataSet.xsd" xmlns:mstns="http://tempuri.org/gatekeeperdbDataSet.xsd" xmlns="http://tempuri.org/gatekeeperdbDataSet.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop" attributeFormDefault="qualified" elementFormDefault="qualified">
    <xs:element name="gatekeeperdbDataSet" msdata:IsDataSet="true" msdata:MainDataTable="http_x003A__x002F__x002F_tempuri.org_x002F_gatekeeperdbDataSet.xsd_x003A_myPrinters" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="myPrinters">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="Name">
                  <xs:simpleType>
                    <xs:restriction base="xs:string">
                      <xs:maxLength value="65535" />
                    </xs:restriction>
                  </xs:simpleType>
                </xs:element>
                <xs:element name="Default" msprop:Generator_ColumnVarNameInTable="columnDefault" msprop:Generator_ColumnPropNameInTable="DefaultColumn" msprop:Generator_UserColumnName="Default" type="xs:boolean" />
                <xs:element name="Connection">
                  <xs:simpleType>
                    <xs:restriction base="xs:string">
                      <xs:maxLength value="65535" />
                    </xs:restriction>
                  </xs:simpleType>
                </xs:element>
                <xs:element name="printerName">
                  <xs:simpleType>
                    <xs:restriction base="xs:string">
                      <xs:maxLength value="65535" />
                    </xs:restriction>
                  </xs:simpleType>
                </xs:element>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    <gatekeeperdbDataSet xmlns="http://tempuri.org/gatekeeperdbDataSet.xsd">
      <myPrinters diffgr:id="myPrinters9" msdata:rowOrder="0">
        <Name>Netserv-stn1</Name>
        <Default>false</Default>
        <Connection>\\SVR-PRINT1\ITOffice-Lsr</Connection>
        <printerName>Network Services</printerName>
      </myPrinters>
    </gatekeeperdbDataSet>
  </diffgr:diffgram>
</myPrintersDataTable>

然而,当我尝试去除这个时,我得到一个xml异常。

A first chance exception of type 'System.InvalidOperationException' occurred in System.Xml.dll

以下是我正在使用的代码......

Private Sub showPrinters(ByRef ms As MemoryStream)
    ms.Position = 0
    Dim myPrinters1 As New DataTable
    Dim deserailizer As New XmlSerializer(myPrinters1.GetType)
    myPrinters1.ReadXml(ms)
    'myPrinters1 = CType(deserailizer.Deserialize(ms), DataTable)

    Dim debugForm As New testoutput
    debugForm.DataGridView1.DataSource = myPrinters1
    debugForm.Show()
End Sub

如你所见。我已经尝试将xml作为流读取,并反序列化,但设置得到相同的错误。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

像这样读取文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            DataSet ds = new DataSet();
            string input = File.ReadAllText(FILENAME);
            StringReader reader = new StringReader(input);
            ds.ReadXml(reader);
        }
    }
}
​