表模式到数据表c#

时间:2011-11-08 01:32:24

标签: c#

如何在c#

中将模式读入数据表
<header>
  <xs:schema id="header" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="header" msdata:IsDataSet="true" msdata:MainDataTable="Table" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="id" type="xs:int" minOccurs="0" />
                <xs:element name="enteredon" type="xs:dateTime" minOccurs="0" />
                <xs:element name="enteredby" type="xs:string" minOccurs="0" />
                <xs:element name="remark" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
</header>

尝试此代码但失败了:

 public static Stream StringToStream(string data)
 {
     try
     {
        System.IO.MemoryStream xmlStream = new System.IO.MemoryStream();
        StreamWriter writer = new StreamWriter(xmlStream);
        writer.Write(data);
        writer.Flush();
        return xmlStream;
     }
     catch (Exception)
     {
         return null;
     }
 }

table.ReadXmlSchema(StringToStream(saveData));

1 个答案:

答案 0 :(得分:4)

您的代码将被抛出&#34;根元素丢失&#34;异常。

我在DaniWeb找到了一个解决方案,即添加以下代码以重置位置。

    System.IO.MemoryStream xmlStream = new System.IO.MemoryStream();
    StreamWriter writer = new StreamWriter(xmlStream);
    writer.Write(data);
    writer.Flush();
    xmlStream.Position = 0;//Add this to reset the position of the stream.

请再试一次。