谈利用XML为数据表添加扩展字段
在数据表中加上ExtendProperty字段,字段的类型为ntext用于存放XML字符串.
生成实体对象,并将ExtendProperty字段移除,将实体类继承至EntityBase.
添加扩展属性的方法;
例:
UserInfo user=new UserInfo();
user["MSN"]=microsoft@microsoft.com;
StaticFunc.DataProvider.UserInfoProvider.Save(user);
如ExtendProperty属性中的XML节点中不存在"MSN"的EntityBase就会在XmlDocument中添加名为"MSN"的节点,值为
microsoft@microsoft.com;如XmlDocument已存在"MSN"节点则修改当前节点中的值。最后在将user对象存入数据库中.
1public class UserInfo:EntityBase
2
3{
4
5
6
7}
8
9public abstract class EntityBase
10
11{
12 XmlDocument doc;
13 public EntityBase()
14 {
15 //初始化doc对象
16 doc= new System.Xml.XmlDocument();
17 System.Xml.XmlDeclaration xdec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
18 doc.AppendChild(xdec);
19 XmlNode iNode = doc.CreateElement(GetType().Name);
20 doc.AppendChild(iNode);
21
22 }
23 //根据节点名称添加或修改值
24 void UpdateExtendProperty(string node,string value)
25 {
26 if (ExtendProperty != string.Empty)//如扩展属性的XML字符不为空字符时doc加载EXTENDPROPERTY
27 {
28 doc.LoadXml(ExtendProperty);
29 }
30 XmlNodeList ns=doc.GetElementsByTagName(GetType().Name);
31 XmlNode n = ns.Item(0);
32 List<string> nodes = new List<string>();
33 foreach (XmlNode item in n.ChildNodes)
34 {
35 nodes.Add(item.Name);
36 }
37 if (nodes.Contains(node))//如节点已存在则调用修改节点值方法
38 {
39 UpdateXMLElement(n, nodes.IndexOf(node), value);
40 }
41 else
42 {
43 if (value != string.Empty)//如节点不存在并且值不为空则对doc对象添加新的节点
44 {
45 CreateXMLElement(n, node, value);
46 //XmlNode cn = doc.CreateNode(XmlNodeType.Element, node, "");
47 //n.AppendChild(cn);
48 //XmlNode valueNode = doc.CreateNode(XmlNodeType.Text, "", "");
49 //valueNode.Value = value;
50 //cn.AppendChild(valueNode);
51 }
52 }
53 if (doc.GetElementsByTagName(GetType().Name).Item(0).ChildNodes.Count > 0)
54 {
55 ExtendProperty = doc.InnerXml;//如DOC中节点为当前类型名称中子节点为为0时将doc的InnerXml字符赋给
56
57ExtendProperty用于存入数据库
58 }
59 else
60 {
61 ExtendProperty = string.Empty;//DOC中节点为当前类型名称中子节点为为0对ExtendProperty赋空字符
62 }
63 }
64 void UpdateXMLElement(XmlNode node,int index,string value)
65 {
66 node.ChildNodes[index].FirstChild.Value = value;
67 if (value == string.Empty)
68 {
69 RemoveExtendProperty(node.ChildNodes[index].Name);
70 }
71 }
72 void CreateXMLElement(XmlNode n,string node,string value)
73 {
74 if (value != string.Empty)
75 {
76 XmlNode cn = doc.CreateNode(XmlNodeType.Element, node, "");
77 n.AppendChild(cn);
78 XmlNode valueNode = doc.CreateNode(XmlNodeType.Text, "", "");
79 valueNode.Value = value;
80 cn.AppendChild(valueNode);
81 }
82 }
83 string GetExtendPropertyByName(string tagName)
84 {
85 if (ExtendProperty != string.Empty)
86 {
87 doc.LoadXml(ExtendProperty);
88 if (doc.GetElementsByTagName(tagName).Count > 0)
89 {
90 return doc.GetElementsByTagName(tagName).Item(0).FirstChild.Value;
91 }
92 }
93 return null;
94 }
95 //用于添加或者修改扩展属性的索引
96 public string this[string propertyName]
97 {
98 set
99 {
100 UpdateExtendProperty(propertyName, value);
101 }
102 get { return GetExtendPropertyByName(propertyName); }
103 }
104 string extendProperty;
105 //扩展属性字段(注:数据库中必须包含ExtendProperty的字段,根据所使用的框架而定)
106 public virtual string ExtendProperty
107 {
108 get { if (extendProperty == null) { return string.Empty; } else { return extendProperty; } }
109 set { extendProperty = value; }
110 }
111 //移除某个子节点
112 public void RemoveExtendProperty(string tagName)
113 {
114 if (ExtendProperty != string.Empty&&ExtendProperty!=null)
115 {
116 doc.LoadXml(ExtendProperty);
117 }
118 XmlNode n=doc.GetElementsByTagName(GetType().Name).Item(0);
119 if (doc.GetElementsByTagName(tagName).Count > 0)
120 {
121 n.RemoveChild(doc.GetElementsByTagName(tagName).Item(0));
122 ExtendProperty = doc.InnerXml;
123 }
124 }
125}
126
2
3{
4
5
6
7}
8
9public abstract class EntityBase
10
11{
12 XmlDocument doc;
13 public EntityBase()
14 {
15 //初始化doc对象
16 doc= new System.Xml.XmlDocument();
17 System.Xml.XmlDeclaration xdec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
18 doc.AppendChild(xdec);
19 XmlNode iNode = doc.CreateElement(GetType().Name);
20 doc.AppendChild(iNode);
21
22 }
23 //根据节点名称添加或修改值
24 void UpdateExtendProperty(string node,string value)
25 {
26 if (ExtendProperty != string.Empty)//如扩展属性的XML字符不为空字符时doc加载EXTENDPROPERTY
27 {
28 doc.LoadXml(ExtendProperty);
29 }
30 XmlNodeList ns=doc.GetElementsByTagName(GetType().Name);
31 XmlNode n = ns.Item(0);
32 List<string> nodes = new List<string>();
33 foreach (XmlNode item in n.ChildNodes)
34 {
35 nodes.Add(item.Name);
36 }
37 if (nodes.Contains(node))//如节点已存在则调用修改节点值方法
38 {
39 UpdateXMLElement(n, nodes.IndexOf(node), value);
40 }
41 else
42 {
43 if (value != string.Empty)//如节点不存在并且值不为空则对doc对象添加新的节点
44 {
45 CreateXMLElement(n, node, value);
46 //XmlNode cn = doc.CreateNode(XmlNodeType.Element, node, "");
47 //n.AppendChild(cn);
48 //XmlNode valueNode = doc.CreateNode(XmlNodeType.Text, "", "");
49 //valueNode.Value = value;
50 //cn.AppendChild(valueNode);
51 }
52 }
53 if (doc.GetElementsByTagName(GetType().Name).Item(0).ChildNodes.Count > 0)
54 {
55 ExtendProperty = doc.InnerXml;//如DOC中节点为当前类型名称中子节点为为0时将doc的InnerXml字符赋给
56
57ExtendProperty用于存入数据库
58 }
59 else
60 {
61 ExtendProperty = string.Empty;//DOC中节点为当前类型名称中子节点为为0对ExtendProperty赋空字符
62 }
63 }
64 void UpdateXMLElement(XmlNode node,int index,string value)
65 {
66 node.ChildNodes[index].FirstChild.Value = value;
67 if (value == string.Empty)
68 {
69 RemoveExtendProperty(node.ChildNodes[index].Name);
70 }
71 }
72 void CreateXMLElement(XmlNode n,string node,string value)
73 {
74 if (value != string.Empty)
75 {
76 XmlNode cn = doc.CreateNode(XmlNodeType.Element, node, "");
77 n.AppendChild(cn);
78 XmlNode valueNode = doc.CreateNode(XmlNodeType.Text, "", "");
79 valueNode.Value = value;
80 cn.AppendChild(valueNode);
81 }
82 }
83 string GetExtendPropertyByName(string tagName)
84 {
85 if (ExtendProperty != string.Empty)
86 {
87 doc.LoadXml(ExtendProperty);
88 if (doc.GetElementsByTagName(tagName).Count > 0)
89 {
90 return doc.GetElementsByTagName(tagName).Item(0).FirstChild.Value;
91 }
92 }
93 return null;
94 }
95 //用于添加或者修改扩展属性的索引
96 public string this[string propertyName]
97 {
98 set
99 {
100 UpdateExtendProperty(propertyName, value);
101 }
102 get { return GetExtendPropertyByName(propertyName); }
103 }
104 string extendProperty;
105 //扩展属性字段(注:数据库中必须包含ExtendProperty的字段,根据所使用的框架而定)
106 public virtual string ExtendProperty
107 {
108 get { if (extendProperty == null) { return string.Empty; } else { return extendProperty; } }
109 set { extendProperty = value; }
110 }
111 //移除某个子节点
112 public void RemoveExtendProperty(string tagName)
113 {
114 if (ExtendProperty != string.Empty&&ExtendProperty!=null)
115 {
116 doc.LoadXml(ExtendProperty);
117 }
118 XmlNode n=doc.GetElementsByTagName(GetType().Name).Item(0);
119 if (doc.GetElementsByTagName(tagName).Count > 0)
120 {
121 n.RemoveChild(doc.GetElementsByTagName(tagName).Item(0));
122 ExtendProperty = doc.InnerXml;
123 }
124 }
125}
126