0

我在数据类型 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。任何人都可以向我指出相关教程或为我提供一些信息吗?泰

4

2 回答 2

0

You can use a class that hold the data you won to export (and import) to xml.
You use the [Serializable] for make the class available for xml export. So for example if you have this class:

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

    public int Value1;

    public int Value2;
}

Then you can use the XmlSerializer to convert it to XML (and vice verse) as:

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;
}

and make the convert to XML as:

MyClassThatKeepTheData cTheObject = new MyClassThatKeepTheData();

ObjectToXML(typeof(MyClassThatKeepTheData), cTheObject)

Relative: How to optimize class for viewstate

and I suggest also the protobuf-net is not XML, but its a lot faster and do the same work

于 2012-12-18T12:31:40.563 回答
0

插入:

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

update [dbo].[TableName]
  set remark.modify ('insert sql:variable("@varibleName") as last into (/remarks/)[1]') WHERE [Condition]
于 2012-12-18T12:29:58.910 回答