📄 messageobject.cs
字号:
namespace Imps.Client.Core
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
public class MessageObject
{
private Dictionary<string, string> _attributes = new Dictionary<string, string>();
private object _syncObject = new object();
private string _text = string.Empty;
private MessageObjectType _type;
private const string localName = "OBJECT";
public static MessageObject FromXML(string xml)
{
MessageObject obj2 = new MessageObject();
using (StringReader input = new StringReader(xml))
{
using (XmlReader reader2 = new XmlTextReader(input))
{
while (reader2.Read())
{
if (reader2.HasAttributes)
{
while (reader2.MoveToNextAttribute())
{
if ((reader2.Name.ToLower() == "type") && Enum.IsDefined(typeof(MessageObjectType), reader2.Value.ToUpper()))
{
Enum.Parse(typeof(MessageObjectType), reader2.Value, true);
}
else
{
obj2.SetAttributeString(reader2.Name.ToUpper(), reader2.Value);
}
}
}
else if (reader2.HasValue)
{
obj2.Text = reader2.Value;
}
}
return obj2;
}
}
}
internal string GetAttributeString(string key)
{
if (this.Attributes.ContainsKey(key.ToUpper()))
{
return this.Attributes.get_Item(key.ToUpper());
}
return string.Empty;
}
internal void SetAttributeString(string key, string value)
{
lock (this._syncObject)
{
if (this.Attributes.ContainsKey(key.ToUpper()))
{
this.Attributes.set_Item(key.ToUpper(), value);
}
else
{
this.Attributes.Add(key.ToUpper(), value);
}
}
}
internal string ToXML()
{
return this.ToXML(null, true);
}
internal string ToXML(List<string> fields, bool ignore)
{
StringWriter w = new StringWriter();
XmlTextWriter writer2 = new XmlTextWriter(w);
writer2.WriteStartElement("OBJECT");
writer2.WriteAttributeString("TYPE", this.Type.ToString());
lock (this._syncObject)
{
Dictionary<string, string>.KeyCollection.Enumerator enumerator = this.Attributes.get_Keys().GetEnumerator();
try
{
while (enumerator.MoveNext())
{
string localName = enumerator.get_Current();
if (((fields == null) || (!fields.Contains(localName) && ignore)) || (fields.Contains(localName) && !ignore))
{
writer2.WriteAttributeString(localName, this.GetAttributeString(localName));
}
}
}
finally
{
enumerator.Dispose();
}
}
if (!string.IsNullOrEmpty(this.Text))
{
writer2.WriteString(this.Text);
}
writer2.WriteEndElement();
return w.ToString();
}
public Dictionary<string, string> Attributes
{
get
{
return this._attributes;
}
set
{
this._attributes = value;
}
}
public string Text
{
get
{
return this._text;
}
set
{
this._text = value;
}
}
public MessageObjectType Type
{
get
{
return this._type;
}
set
{
this._type = value;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -