How to read XML into a DataTable?

后端 未结 7 1590
星月不相逢
星月不相逢 2020-12-16 02:51

I have some XML in a string in memory exactly like this:


  EURCHF
  EURGBP

        
相关标签:
7条回答
  • 2020-12-16 03:18

    Use dataset in place of datatable

    0 讨论(0)
  • 2020-12-16 03:31

    From here: http://www.dreamincode.net/code/snippet3186.htm

    // <summary>
    /// method for reading an XML file into a DataTable
    /// </summary>
    /// <param name="file">name (and path) of the XML file</param>
    /// <returns></returns>
    public DataTable ReadXML(string file)
    {
        //create the DataTable that will hold the data
        DataTable table = new DataTable("XmlData");
        try
        {
            //open the file using a Stream
            using(Stream stream = new  FileStream(file, FileMode.Open, FileAccess.Read))
            {
                //create the table with the appropriate column names
                table.Columns.Add("Name", typeof(string));
                table.Columns.Add("Power", typeof(int));
                table.Columns.Add("Location", typeof(string));
    
                //use ReadXml to read the XML stream
                table.ReadXml(stream);
    
                //return the results
                return table;
            }                
        }
        catch (Exception ex)
        {
            return table;
        }
    }
    

    You might want to take a look at DataTable.ReadXml method.

    EDIT: If you have xml object in memory you can use the ReadXml method directly. DataTable.ReadXml(MemoryStream Object);

    EDIT 2: I did the export. The following XML Schema is required:

    <?xml version="1.0" standalone="yes"?>
    <DocumentElement>
      <symbols>
        <symbol>EURCHF</symbol>
      </symbols>
      <symbols>
        <symbol>EURGBP</symbol>
      </symbols>
      <symbols>
        <symbol>EURJPY</symbol>
      </symbols>
    </DocumentElement>
    
    0 讨论(0)
  • 2020-12-16 03:31

    Another way:

    public DataTable ReadXML(string yourPath)
            {
                DataTable table = new DataTable("Item");
                try
                {
                    DataSet lstNode = new DataSet();
                    lstNode.ReadXml(yourPath);
                    table = lstNode.Tables["Item"];
                    return table;
                }
                catch (Exception ex)
                {
                    return table;
                }
            }

    And here's XML format:

    <?xml version="1.0" encoding="utf-8" ?>
    <db>
      <Item>
        <Id>222</Id>
        <OldCode>ZA</OldCode>
        <NewCode>ZAF</NewCode>
        <Name>Africa (South )</Name>
      </Item>
    </db>

    0 讨论(0)
  • 2020-12-16 03:32

    Dynamic Xml to DataTable

    public DataTable XMLToDataTable(string YourFilePath)
    {
            DataTable table = new DataTable("XMLTABLE");
            try
            {
                #region &quot;&apos;< &lt;> &gt;&amp NOT VALID EXTENSTION IN XML
                var xmlContent = File.ReadAllText(YourFilePath);
                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(xmlContent.Replace("'", "&apos;").Replace("&", "&amp;"));
                xDoc.Save(YourFilePath);
                #endregion
                //All XML Document Content
                //XmlElement root = xDoc.DocumentElement;
                string RootNode = xDoc.DocumentElement.Name;
                string RootChildNode = xDoc.DocumentElement.LastChild.Name;
                DataSet lstNode = new DataSet();
                lstNode.ReadXml(YourFilePath);
                table = lstNode.Tables[RootChildNode];
                return table;
            }
            catch (Exception ex)
            {
                
            }
    }
    
    0 讨论(0)
  • 2020-12-16 03:33

    Here is a sample-code in Powershell:

    $xmlString = @"
    <table1>
        <row1>
            <c1>value1</c1><c2>value2</c2>
        </row1>
        <row2>
            <c1>value3</c1><c2>value4</c2>
        </row2>
    </table1>
    "@
    $sr =[System.IO.StringReader]($xmlString)
    
    $dataset =[System.Data.DataSet]::new()
    $null = $dataset.ReadXml($sr)
    

    and this is the result of $dataset.tables:

    c1     c2    
    --     --    
    value1 value2
    value3 value4
    
    0 讨论(0)
  • 2020-12-16 03:40

    Like this:

    Dim strXmlString As String = "<tables><row><table_name>Table1</table_name><record_key>1</record_key></row>"
    strXmlString += "<row><table_name>Table2</table_name><record_key>2</record_key></row></tables>"
    Dim srXMLtext As System.IO.StringReader = New System.IO.StringReader(strXmlString)
    
    Dim dt As New DataTable
    dt.ReadXml(srXMLtext)
    
    0 讨论(0)
提交回复
热议问题