wpf DataTable 或 DataSet 的 XML 反序列化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17360342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:06:02  来源:igfitidea点击:

XML Deserialization of DataTable or DataSet

c#wpfxmlserializationdatatable

提问by Adam Szabo

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). Source=System.Xml

System.InvalidOperationException 未处理 Message=XML 文档中存在错误 (26, 14)。源=系统.Xml

InnerException:

内部异常:

"http://www.w3.org/2001/XMLSchema'> was not expected."

“http://www.w3.org/2001/XMLSchema'> 不是预期的。”

How can I deserialize that thing?

我怎样才能反序列化那个东西?

回答by Adam Szabo

Managed to solve it. Seems like when we directly read to an existing DataSetusing 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]

回答by Mataniko

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.aspxand notice the different ways to initalize it.

查看 XmlSerializer.Deserialize 的文档:http: //msdn.microsoft.com/en-us/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. Even easier you can just pass it the file/memory stream directly.

简单地不要开始阅读 XML 并通过反序列化你的阅读器。更简单的是,您可以直接将文件/内存流传递给它。