【问题标题】:How to store data from XML in SQL server.如何在 SQL Server 中存储 XML 中的数据。
【发布时间】:2014-04-27 07:10:57
【问题描述】:

我有一个 xml 格式的网址。

示例xml文件格式如下:

            <shows>
            <show>
                <eventID>10025</eventID>
                <name>MARCH Barrel</name>

                <ticketURL></ticketURL>

                    <venue>
                        <venueID>3648</venueID>
                        <name>Double Barrel</name>
                        <address>3770 Las Vegas Blvd. South</address>
                        <address2></address2>
                        <city>Las Vegas</city>
                        <state>NV</state>
                        <zipcode>89109</zipcode>
                        <country>US</country>
                    </venue>
                      </show>
                </shows>

问题: 如何,是否可以使用 C# 在 sql server 2008 中存储场地 ID 和名称,因为我有一个包含此类字段的表?

【问题讨论】:

  • 这个xml是由一个url生成的。如何获取数据集中的xml数据或其他东西。 & 然后在数据库中只存储 id 和 name

标签: asp.net sql-server xml c#-4.0


【解决方案1】:

您可以在 SQL Server XML 类型字段中存储信息。以下文章包含有关如何在 sql server 中使用 XML 数据类型的完整信息。

https://www.simple-talk.com/sql/learn-sql-server/working-with-the-xml-data-type-in-sql-server/

【讨论】:

    【解决方案2】:

    SQL Server 2005 及更高版本有一个名为“XML”的数据类型,您可以在其中存储 XML - 无类型或使用 XSD 架构键入。

    您基本上可以从 XML 文字字符串中填充 XML 类型的列,因此您可以轻松地使用普通的 INSERT 语句并将 XML 内容填充到该字段中。

    您可以使用函数 OPENXML 和存储过程 sp_xml_preparedocument 轻松地将您的 XML 转换为行集。

    编辑:试试这个

    String xmlFile = @" <shows>
                <show>
                    <eventID>10025</eventID>
                    <name>MARCH Barrel</name>
    
                    <ticketURL></ticketURL>
    
                        <venue>
                            <venueID>3648</venueID>
                            <name>Double Barrel</name>
                            <address>3770 Las Vegas Blvd. South</address>
                            <address2></address2>
                            <city>Las Vegas</city>
                            <state>NV</state>
                            <zipcode>89109</zipcode>
                            <country>US</country>
                        </venue>
                          </show>
                    </shows>";
    
    
                    DataSet ds = ConvertXMLToDataSet(xmlFile);
    
                foreach (DataTable dt in ds.Tables)
                {
                    //get Your Tables here
                }
    

    和方法:

    public static 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();
                    }
                }
    

    【讨论】:

    • 感谢您的回复。看我有一个由这种类型的 xml 组成的 url。首先是如何在数据集中获取所有这些值,以便我选择要存储在数据库中的数据。
    • 收到此错误:- System.Xml.dll 中出现“System.Xml.XmlException”类型的未处理异常附加信息:“”,十六进制值 0x03,是无效字符。第 3854 行,位置 34。
    • 查看示例代码 XmlTextReader reader = new XmlTextReader("news.yahoo.com/rss/"); DataSet ds = new DataSet(); 这样我现在从雅虎获得新闻提要,同样我希望我的 xml url 返回数据在数据集或其他东西中保存它然后我根据需要将数据拆分到数据库 ds.ReadXml(reader); grid.DataSource = ds; grid.DataBind();
    • 你的问题不是关于那个。现在你想做不同的事情。我做了你想说的
    猜你喜欢
    • 1970-01-01
    • 2020-11-18
    • 2012-11-18
    • 2017-08-06
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 2010-10-26
    相关资源
    最近更新 更多