如何将xml数据转换为数据集

时间:2010-06-25 05:12:01

标签: vb.net

如何将以下xml数据转换为vb.net中的数据集?

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<userinfolist>    
    <UserInfo>        
        <LastName>Thukalil</LastName>        
        <FirstName>Thariq</FirstName>        
        <Email>test1@test.com</Email>        
        <Courses>            
            <Course>                
                <CourseNumber>av51198</CourseNumber>                
                <CourseTitle>silverlight test course</CourseTitle>                
                <Format>On Demand</Format>                
                <CompletionDate>2009-04-20T10:46:12-05:00</CompletionDate>   
                    <Credits>                    
                        <CreditInfo>                        
                            <State>Missouri</State>                        
                            <CreditType>General Credits</CreditType>                        
                            <CreditAmount>1.6</CreditAmount>                        
                            <CreditDetail>Self-Study</CreditDetail>
                        </CreditInfo>                    
                        <CreditInfo>                        
                            <State>New York</State>                        
                            <CreditType>General Credits</CreditType>                        
                            <CreditAmount>1.5</CreditAmount>                        
                            <CreditDetail>Participatory Online</CreditDetail>                    
                        </CreditInfo>                    
                        <CreditInfo>                       
                            <State>Alaska</State>                        
                            <CreditType>General Credits</CreditType>                        
                            <CreditAmount>1.25</CreditAmount>                        
                            <CreditDetail>Online</CreditDetail>                    
                        </CreditInfo>                
                    </Credits>            
            </Course>        
        </Courses>    
    </UserInfo>    
    <UserInfo>        
        <LastName>Tammana</LastName>        
        <FirstName>Pavan</FirstName>        
        <Email>test2@test.com</Email>        
        <Courses>            
            <Course>                
                <CourseNumber>av51540</CourseNumber>                
                <CourseTitle>Flex LWC 10 29 Test</CourseTitle>                
                <Format>Live</Format>                
                <CompletionDate>2009-10-29T14:31:21-05:00</CompletionDate>              
                <Credits>                    
                    <CreditInfo>                        
                        <State>Alabama</State>                        
                        <CreditType>General Credits</CreditType>                        
                        <CreditAmount>1.0</CreditAmount>                        
                        <CreditDetail>Live Event</CreditDetail>                    
                    </CreditInfo>               
                </Credits>       
            </Course>      
        </Courses>   
    </UserInfo>
</userinfolist>

请帮忙。感谢。

2 个答案:

答案 0 :(得分:2)

使用Dataset.ReadXml()。这可以将任何xml字符串,xml流或xml文件转换为数据集。

http://msdn.microsoft.com/en-us/library/system.data.dataset.readxml.aspx

答案 1 :(得分:1)

以下代码可用于将XML存储到数据集中:

// Function to convert passed XML data to dataset
  public DataSet ConvertXMLToDataSet(string xmlData)
  {
   StringReader stream = null;
   XmlTextReader reader = null;
   try
   {
    DataSet xmlDS = new DataSet() ;
    stream = new StringReader(xmlData);
    // Load the XmlTextReader from the stream
    reader = new XmlTextReader(stream);
    xmlDS.ReadXml(reader);
    return xmlDS;
   }
   catch
   {
    return null;
   }
   finally
   {
    if(reader != null) reader.Close();
   }
  }// Use this function to get XML string from a dataset

  // Function to convert passed dataset to XML data
  public string ConvertDataSetToXML(DataSet xmlDS)
  {
   MemoryStream stream = null;
   XmlTextWriter writer = null;
   try
   {
    stream = new MemoryStream();
    // Load the XmlTextReader from the stream
    writer = new XmlTextWriter(stream, Encoding.Unicode);
    // Write to the file with the WriteXml method.
    xmlDS.WriteXml(writer);
    int count = (int) stream.Length;
    byte[] arr = new byte[count];
    stream.Seek(0, SeekOrigin.Begin);
    stream.Read(arr, 0, count);
    UnicodeEncoding utf = new UnicodeEncoding();
    return utf.GetString(arr).Trim();
   }
   catch
   {
    return String.Empty ;
   }
   finally
   {
    if(writer != null) writer.Close();
   }
  }

替代参考:http://support.microsoft.com/kb/311566