웹 서비스에 포함시킬 수있는 키/값 쌍 객체를 찾고 있습니다.
.NET의 System.Collections.Generic.KeyValuePair<>
클래스이지만 웹 서비스에서 올바르게 직렬화되지 않습니다. 웹 서비스에서 Key 및 Value 속성은 직렬화되지 않으므로 누군가이 문제를 해결하는 방법을 모르는 경우이 클래스를 사용할 수 없습니다.
이 상황에 사용할 수있는 다른 일반 클래스가 있습니까?
.NET의 System.Web.UI.Pair
클래스이지만 유형에 Object를 사용합니다. 타입 안전을 위해서만 Generic 클래스를 사용하는 것이 좋습니다.
구조체/클래스를 정의하십시오.
[Serializable]
public struct KeyValuePair<K,V>
{
public K Key {get;set;}
public V Value {get;set;}
}
Dictionary<>
자체가 XML 직렬화 가능하지 않다고 생각하지 않습니다. 웹 서비스를 통해 사전 객체를 보내야 할 때 Dictionary<>
객체를 직접 감싸고 지원을 추가했습니다. IXMLSerializable
의 경우.
/// <summary>
/// Represents an XML serializable collection of keys and values.
/// </summary>
/// <typeparam name="TKey">The type of the keys in the dictionary.</typeparam>
/// <typeparam name="TValue">The type of the values in the dictionary.</typeparam>
[XmlRoot("dictionary")]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
{
#region Constants
/// <summary>
/// The default XML tag name for an item.
/// </summary>
private const string DEFAULT_ITEM_TAG = "Item";
/// <summary>
/// The default XML tag name for a key.
/// </summary>
private const string DEFAULT_KEY_TAG = "Key";
/// <summary>
/// The default XML tag name for a value.
/// </summary>
private const string DEFAULT_VALUE_TAG = "Value";
#endregion
#region Protected Properties
/// <summary>
/// Gets the XML tag name for an item.
/// </summary>
protected virtual string ItemTagName
{
get
{
return DEFAULT_ITEM_TAG;
}
}
/// <summary>
/// Gets the XML tag name for a key.
/// </summary>
protected virtual string KeyTagName
{
get
{
return DEFAULT_KEY_TAG;
}
}
/// <summary>
/// Gets the XML tag name for a value.
/// </summary>
protected virtual string ValueTagName
{
get
{
return DEFAULT_VALUE_TAG;
}
}
#endregion
#region Public Methods
/// <summary>
/// Gets the XML schema for the XML serialization.
/// </summary>
/// <returns>An XML schema for the serialized object.</returns>
public XmlSchema GetSchema()
{
return null;
}
/// <summary>
/// Deserializes the object from XML.
/// </summary>
/// <param name="reader">The XML representation of the object.</param>
public void ReadXml(XmlReader reader)
{
XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
bool wasEmpty = reader.IsEmptyElement;
reader.Read();
if (wasEmpty)
{
return;
}
while (reader.NodeType != XmlNodeType.EndElement)
{
reader.ReadStartElement(ItemTagName);
reader.ReadStartElement(KeyTagName);
TKey key = (TKey)keySerializer.Deserialize(reader);
reader.ReadEndElement();
reader.ReadStartElement(ValueTagName);
TValue value = (TValue)valueSerializer.Deserialize(reader);
reader.ReadEndElement();
this.Add(key, value);
reader.ReadEndElement();
reader.MoveToContent();
}
reader.ReadEndElement();
}
/// <summary>
/// Serializes this instance to XML.
/// </summary>
/// <param name="writer">The writer to serialize to.</param>
public void WriteXml(XmlWriter writer)
{
XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
foreach (TKey key in this.Keys)
{
writer.WriteStartElement(ItemTagName);
writer.WriteStartElement(KeyTagName);
keySerializer.Serialize(writer, key);
writer.WriteEndElement();
writer.WriteStartElement(ValueTagName);
TValue value = this[key];
valueSerializer.Serialize(writer, value);
writer.WriteEndElement();
writer.WriteEndElement();
}
}
#endregion
}
KeyValuePairs를 직렬화 할 수없는 이유는 다음과 같습니다. MSDN 블로그 게시물
Struct 답변은 가장 간단한 솔루션이지만 유일한 솔루션은 아닙니다. "더 나은"솔루션은 직렬화 가능한 사용자 정의 KeyValurPair 클래스를 작성하는 것입니다.
[Serializable]
public class SerializableKeyValuePair<TKey, TValue>
{
public SerializableKeyValuePair()
{
}
public SerializableKeyValuePair(TKey key, TValue value)
{
Key = key;
Value = value;
}
public TKey Key { get; set; }
public TValue Value { get; set; }
}
4.0 프레임 워크에는 직렬화 가능하고 동일 할 수있는 Tuple 클래스 클래스가 추가되었습니다. Tuple.Create(a, b)
또는 new Tuple<T1, T2>(a, b)
을 사용할 수 있습니다.
키 값 쌍을 처리 할 수 있으므로 DataContractSerializer를 사용하십시오.
public static string GetXMLStringFromDataContract(object contractEntity)
{
using (System.IO.MemoryStream writer = new System.IO.MemoryStream())
{
var dataContractSerializer = new DataContractSerializer(contractEntity.GetType());
dataContractSerializer.WriteObject(writer, contractEntity);
writer.Position = 0;
var streamReader = new System.IO.StreamReader(writer);
return streamReader.ReadToEnd();
}
}
KeyedCollection은 넌센스없이 xml로 직접 직렬화 할 수있는 사전 유형입니다. 유일한 문제는 다음 방법으로 값에 액세스해야한다는 것입니다. coll [ "key"]. Value;
DataTable
은 JSON으로 직렬화 할 데이터를 (단독으로) 래핑하기 위해 내가 가장 좋아하는 모음입니다. 추가 struct
없이도 쉽게 확장 할 수 있으며 Tuple<>[]
가장 깨끗한 방법은 아니지만 새 struct
를 선언하는 대신 클래스에 직접 포함하여 사용하는 것이 좋습니다 (직렬화 됨).
class AnyClassToBeSerialized
{
public DataTable KeyValuePairs { get; }
public AnyClassToBeSerialized
{
KeyValuePairs = new DataTable();
KeyValuePairs.Columns.Add("Key", typeof(string));
KeyValuePairs.Columns.Add("Value", typeof(string));
}
public void AddEntry(string key, string value)
{
DataRow row = KeyValuePairs.NewRow();
row["Key"] = key; // "Key" & "Value" used only for example
row["Value"] = value;
KeyValuePairs.Rows.Add(row);
}
}
XmlSerializer는 사전에서 작동하지 않습니다. 아, KeyValuePairs에도 문제가 있습니다
http://www.codeproject.com/Tips/314447/XmlSerializer-doesnt-work-with-Dictionaries-Oh-and