熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#高級編程:寫入XML輸出結果

2013-11-13 11:01:54  來源: .NET編程 
    ——此文章摘自《C#高級編程(第版)》定價元 特價元 購買

    如上所述DataSet支持在XML中定義其模式可以從XML文檔中讀取數據也可以把數據寫入XML文檔

    DataSetWriteXml方法可以輸出存儲在DataSet中的各種數據可以選擇只輸出數據也可以輸出數據和模式下面是為上述Region示例編寫的代碼
    dsWriteXml(\\WithoutSchemaxml);
    dsWriteXml(\\WithSchemaxml XmlWriteModeWriteSchema);

    第一個文件WithoutSchemaxml如下所示
    <?xml version= standalone=yes?>
    <NewDataSet>
       <Region>
          <RegionID></RegionID>
          <RegionDescription>Eastern                       </RegionDescription>
       </Region>
       <Region>
          <RegionID></RegionID>
          <RegionDescription>Western                       </RegionDescription>
       </Region>
       <Region>
          <RegionID></RegionID>
          <RegionDescription>Northern                      </RegionDescription>
       </Region>
       <Region>
          <RegionID></RegionID>
          <RegionDescription>Southern                      </RegionDescription>
       </Region>
    </NewDataSet>

    RegionDescription上的閉合標記在頁面的右邊因為數據庫列定義為NCHAR()這是一個包含個字符的字符串其中用空格填充

    WithSchemaxml文件生成的結果包含DataSet的XML模式和數據
    <?xml version= standalone=yes?>
    <NewDataSet>
       <xs:schema id=NewDataSet xmlns=
                  xmlns:xs=http://wwwworg//XMLSchema
                  xmlns:msdata=urn:schemasmicrosoftcom:xmlmsdata>
          <xs:element FTEL=NewDataSet msdata:IsDataSet=true>
             <xs:complexType>
                <xs:choice maxOccurs=unbounded>
                   <xs:element FTEL=Region>
                      <xs:complexType>
                         <xs:sequence>
                            <xs:element FTEL=RegionID
                                        msdata:AutoIncrement=true
                                        msdata:AutoIncrementSeed=
                                        type=xs:int />
                            <xs:element FTEL=RegionDescription
                                        type=xs:string />
                         </xs:sequence>
                      </xs:complexType>
                   </xs:element>
                </xs:choice>
             </xs:complexType>
          </xs:element>
       </xs:schema>
       <Region>
          <RegionID></RegionID>
          <RegionDescription>Eastern                       </RegionDescription>
       </Region>
       <Region>
          <RegionID></RegionID>
          <RegionDescription>Western                       </RegionDescription>
       </Region>
       <Region>
          <RegionID></RegionID>
          <RegionDescription>Northern                     </RegionDescription>
       </Region>
       <Region>
          <RegionID></RegionID>
          <RegionDescription>Southern                     </RegionDescription>
       </Region>
    </NewDataSet>

    注意使用msdata模式中的文件定義DataSet中列的附加屬性例如AutoIncrement 和 AutoIncrementSeed這些屬性直接對應於DataColumn上的可定義屬性


From:http://tw.wingwit.com/Article/program/net/201311/15028.html
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.