【问题标题】:Inserting and Selecting XML data in SQL Server with ASP.NET (C#)使用 ASP.NET (C#) 在 SQL Server 中插入和选择 XML 数据
【发布时间】:2012-12-18 12:14:57
【问题描述】:

我在数据类型 XML 的 SQL Server 数据库表中有一个字段。

建议格式如下:

<remarks>
    <remark>
        <author>Patrick Keane</author>
        <date>18/12/2012 10:06</date>
        <content>My content Here</content>
    </remark>
    <remark>
        <author>Joe Blogs</author>
        <date>19/12/2012 11:32</date>
        <content>My content Here</content>
    </remark>
    ......
    ......
</remarks>

使用 ASP.NET (C#),我想向该字段添加一个新条目(其中可能已经或可能没有条目)。 我还希望能够检索最后一个条目(用于显示在我的 .aspx 页面上)和完整的条目列表(用于显示在 .xml 页面上)。

我一直在寻找信息/教程,我发现的大部分内容是导出数据并转换为 XML。任何人都可以向我指出相关教程或为我提供一些信息吗?类型

【问题讨论】:

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


    【解决方案1】:

    插入:

    将要附加到存储过程中的 xml 作为参数传递,然后尝试以下查询:

    update [dbo].[TableName]
      set remark.modify ('insert sql:variable("@varibleName") as last into (/remarks/)[1]') WHERE [Condition]
    

    【讨论】:

      【解决方案2】:

      您可以使用一个类来保存您赢得的数据以导出(和导入)到 xml。
      您使用 [Serializable] 使该类可用于 xml 导出。例如,如果你有这个类:

      [Serializable]
      public class MyClassThatKeepTheData
      {
          public List<int> cListWithValues;
      
          public int Value1;
      
          public int Value2;
      }
      

      然后您可以使用 XmlSerializer 将其转换为 XML(反之亦然):

      public static string ObjectToXML(Type type, object obby)
      {
          XmlSerializer ser = new XmlSerializer(type);
          using (System.IO.MemoryStream stm = new System.IO.MemoryStream())
          {
              //serialize to a memory stream
              ser.Serialize(stm, obby);
              //reset to beginning so we can read it.  
              stm.Position = 0;
              //Convert a string. 
              using (System.IO.StreamReader stmReader = new System.IO.StreamReader(stm))
              {
                  string xmlData = stmReader.ReadToEnd();
                  return xmlData;
              }
          }
      }
      
      public static object XmlToObject(Type type, string xml)
      {
          object oOut = null;    
      
          if (xml != null && xml.Length > 0)
          {
              System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(type);
      
              using (System.IO.StringReader sReader = new System.IO.StringReader(xml))
              {
                  oOut = serializer.Deserialize(sReader);
      
                  sReader.Close();
              }
          }
      
          return oOut;
      }
      

      并将转换为 XML 为:

      MyClassThatKeepTheData cTheObject = new MyClassThatKeepTheData();
      
      ObjectToXML(typeof(MyClassThatKeepTheData), cTheObject)
      

      亲戚: How to optimize class for viewstate

      我还建议 protobuf-net 不是 XML,但它的速度要快得多,并且可以做同样的工作

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-16
        • 2014-10-26
        • 2014-10-05
        • 2018-01-10
        相关资源
        最近更新 更多