简体   繁体   English

DataTable或DataSet的XML反序列化

[英]XML Deserialization of DataTable or DataSet

I have the following XML: 我有以下XML:

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

I know this was created by serializing a DataTable with new XmlSerializer(typeof(DataTable)).Serialize(writer, (DataTable)myDataTable); 我知道这是通过使用new XmlSerializer(typeof(DataTable)).Serialize(writer, (DataTable)myDataTable); .

The schema as well as the actual data can be different in them. 架构以及实际数据可以在其中不同。

I need to deserialize it, and tried the following: 我需要反序列化它,并尝试以下方法:

reader.ReadStartElement("NewDataSet");
var dataSet = (DataTable)new XmlSerializer(typeof(DataTable)).Deserialize(reader);
reader.ReadEndElement();

and also: 并且:

reader.ReadStartElement("NewDataSet");
var dataSet = (DataSet)new XmlSerializer(typeof(DataSet)).Deserialize(reader);
reader.ReadEndElement();

All I got was 我只有

System.InvalidOperationException was unhandled Message=There is an error in XML document (26, 14). System.InvalidOperationException未处理Message = XML文档中存在错误(26,14)。 Source=System.Xml 来源=的System.Xml

InnerException: 的InnerException:

"http://www.w3.org/2001/XMLSchema'> was not expected." “不应预期http://www.w3.org/2001/XMLSchema'>。”

How can I deserialize that thing? 我怎样才能反序化那个东西?

Managed to solve it. 管理解决它。 Seems like when we directly read to an existing DataSet using its ReadXml() method, it just works. 似乎当我们直接读取到一个现有类似DataSet使用其ReadXml()方法,它只是工作。

var dataSet = new DataSet();
dataSet.ReadXml(reader);

Now I can reach the table as dataSet.Tables[0] 现在我可以作为dataSet.Tables[0]到达表

I can't really provide you with sample code as I don't have context to the rest of your code. 我无法真正为您提供示例代码,因为我没有其他代码的上下文。

Look at the documentation to XmlSerializer.Deserialize at: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.deserialize.aspx and notice the different ways to initalize it. 请参阅XmlSerializer.Deserialize的文档, 网址为: http : //msdn.microsoft.com/zh-cn/library/system.xml.serialization.xmlserializer.deserialize.aspx,并注意了初始化它的不同方法。

Your reader should be at the beginning of the stream, which it is not after you called 您的读者应该在流的开头,而不是在您打电话之后

reader.ReadStartElement("NewDataSet");

Simply don't begin reading the XML and just pass Deserialize your reader. 只是不要开始阅读XML,只是通过反序列化您的读者。 Even easier you can just pass it the file/memory stream directly. 更简单的是你可以直接传递文件/内存流。

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

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