forked from cyq1162/cyqdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XHtmlAction.cs
1409 lines (1356 loc) · 52 KB
/
XHtmlAction.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Xml;
using CYQ.Data.Table;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using CYQ.Data.Tool;
using System.Text;
using System.Web;
namespace CYQ.Data.Xml
{
/// <summary>
/// Xml/Html操作类
/// </summary>
public partial class XHtmlAction : XHtmlBase
{
#region 构造函数
/// <summary>
/// 默认构造函数[操作无名称空间的Xml]
/// </summary>
public XHtmlAction()
: base()
{
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="isForHtml">true时,将自动载入html的名称空间(http://www.w3.org/1999/xhtml)</param>
public XHtmlAction(bool isForHtml)
: base()
{
if (isForHtml)
{
base.LoadNameSpace(htmlNameSpace);
}
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="isForHtml">true时,将自动载入html的名称空间(http://www.w3.org/1999/xhtml)</param>
/// <param name="isNoClone">true时文档应为只读,所获取是同一份文档引用;false时文档可写,每次获取会克隆一份文档返回。</param>
public XHtmlAction(bool isForHtml, bool isNoClone)
: base()
{
if (isForHtml)
{
base.LoadNameSpace(htmlNameSpace);
}
IsNoClone = isNoClone;
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="nameSpaceUrl">当Xml的名称空间[若有]</param>
public XHtmlAction(string nameSpaceUrl)
: base()
{
base.LoadNameSpace(nameSpaceUrl);
}
#endregion
#region 查询
/// <summary>
/// GetByID or GetByName
/// </summary>
/// <param name="idOrName">id or name</param>
/// <returns></returns>
public XmlNode Get(string idOrName)
{
return Get(idOrName, null);
}
public XmlNode Get(string idOrName, XmlNode parentNode)
{
XmlNode node = GetByID(idOrName, parentNode);
if (node == null)
{
node = GetByName(idOrName, parentNode);
if (node == null)
{
switch (idOrName.ToLower())
{
case "head":
case "body":
case "title":
case "form":
case "style":
case "meta":
case "link":
case "script":
XmlNodeList xList = GetList(idOrName.ToLower(), parentNode);
if (xList != null)
{
node = xList[0];
}
break;
}
}
}
return node;
}
public XmlNode GetByID(string id)
{
return Fill(GetXPath("*", "id", id), null);
}
public XmlNode GetByID(string id, XmlNode parentNode)
{
return Fill(GetXPath("*", "id", id), parentNode);
}
public XmlNode GetByName(string name)
{
return Fill(GetXPath("*", "name", name), null);
}
public XmlNode GetByName(string name, XmlNode parentNode)
{
return Fill(GetXPath("*", "name", name), parentNode);
}
public XmlNode Get(string tag, string attr, string value, XmlNode parentNode)
{
return Fill(GetXPath(tag, attr, value), parentNode);
}
public XmlNodeList GetList(string tag, string attr, string value)
{
return Select(GetXPath(tag, attr, value), null);
}
public XmlNodeList GetList(string tag, string attr, string value, XmlNode parentNode)
{
return Select(GetXPath(tag, attr, value), parentNode);
}
public XmlNodeList GetList(string tag, string attr)
{
return Select(GetXPath(tag, attr, null), null);
}
public XmlNodeList GetList(string tag, string attr, XmlNode parentNode)
{
return Select(GetXPath(tag, attr, null), parentNode);
}
public XmlNodeList GetList(string tag)
{
return Select(GetXPath(tag, null, null), null);
}
public XmlNodeList GetList(string tag, XmlNode parentNode)
{
return Select(GetXPath(tag, null, null), parentNode);
}
#endregion
#region 创建
public void CreateNodeTo(XmlNode parentNode, string tag, string text, params string[] attrAndValue)
{
if (parentNode != null)
{
parentNode.AppendChild(CreateNode(tag, text, attrAndValue));
}
}
public XmlNode CreateNode(string tag, string text, params string[] attrAndValue)
{
XmlElement xElement = Create(tag);
try
{
xElement.InnerXml = text;
}
catch
{
xElement.InnerXml = SetCDATA(text);
}
if (attrAndValue != null && attrAndValue.Length % 2 == 0)
{
string attr = "", value = "";
for (int i = 0; i < attrAndValue.Length; i++)
{
attr = attrAndValue[i];
i++;
value = attrAndValue[i];
xElement.SetAttribute(attr, value);
}
}
return xElement as XmlNode;
}
#endregion
#region 附加
public void AppendNode(XmlNode parentNode, XmlNode childNode)
{
if (parentNode != null && childNode != null)
{
parentNode.AppendChild(childNode);
}
}
/// <summary>
/// 添加节点
/// </summary>
/// <param name="position">parentNode的第N个子节点之后</param>
public void AppendNode(XmlNode parentNode, XmlNode childNode, int position)
{
if (parentNode != null && childNode != null)// A B
{
if (parentNode.ChildNodes.Count == 0 || position >= parentNode.ChildNodes.Count)
{
parentNode.AppendChild(childNode);
}
else if (position == 0)
{
InsertBefore(childNode, parentNode.ChildNodes[0]);
}
else
{
InsertAfter(childNode, parentNode.ChildNodes[position - 1]);
}
}
}
#endregion
#region 删除
/// <summary>
/// 保留节点,但清除节点所内容/属性
/// </summary>
public void Clear(XmlNode node)
{
node.RemoveAll();
}
public void Remove(XmlNode node)
{
if (node != null)
{
node.ParentNode.RemoveChild(node);
}
}
public void Remove(string idOrName)
{
XmlNode node = Get(idOrName);
if (node != null)
{
node.ParentNode.RemoveChild(node);
}
}
public void RemoveAllChild(XmlNode node)
{
RemoveChild(node, 0);
}
public void RemoveAllChild(string idOrName)
{
RemoveChild(idOrName, 0);
}
/// <summary>
/// 移除子节点
/// </summary>
/// <param name="id">节点的id</param>
/// <param name="start">从第几个子节点开始删除[索引从0开始]</param>
public void RemoveChild(string idOrName, int start)
{
XmlNode node = Get(idOrName);
if (node != null)
{
RemoveChild(node, start);
}
}
/// <summary>
/// 移除子节点
/// </summary>
/// <param name="node">节点</param>
/// <param name="start">从第几个子节点开始删除[索引从0开始]</param>
public void RemoveChild(XmlNode node, int start)
{
if (start == 0)
{
node.InnerXml = "";
return;
}
if (node.ChildNodes.Count > start) //1个子节点, 0
{
for (int i = node.ChildNodes.Count - 1; i >= start; i--)
{
node.RemoveChild(node.ChildNodes[i]);
}
}
}
/// <summary>
/// 移除多个属性
/// </summary>
/// <param name="ids">要移除的名称列表</param>
public void RemoveAttrList(params string[] attrNames)
{
XmlNodeList nodeList = null;
foreach (string name in attrNames)
{
nodeList = GetList("*", name);
if (nodeList != null && nodeList.Count > 0)
{
for (int i = 0; i < nodeList.Count; i++)
{
nodeList[i].Attributes.Remove(nodeList[i].Attributes[name]);
}
}
}
}
/// <summary>
/// 属性移除
/// </summary>
/// <param name="attrName">属性名称</param>
/// <param name="excludeSetType">排除的节点类型</param>
public void RemoveAttrList(string attrName, SetType excludeSetType)
{
XmlNodeList nodeList = GetList("*", attrName);
if (nodeList != null && nodeList.Count > 0)
{
XmlNode node = null;
string setType = excludeSetType.ToString().ToLower();
for (int i = 0; i < nodeList.Count; i++)
{
node = nodeList[i];
if (node.Name != setType)
{
node.Attributes.Remove(node.Attributes[attrName]);
}
}
}
}
/// <summary>
/// 移除注释节点
/// </summary>
/// <param name="node">移除此节点的注释文本</param>
public void RemoveCommentNode(XmlNode node)
{
if (node != null)
{
XmlNodeList xmlNodeList = Select("//comment()", node);
foreach (XmlNode xNode in xmlNodeList)
{
xNode.ParentNode.RemoveChild(xNode);
}
}
}
/// <summary>
/// 移除注释节点
/// </summary>
public override void RemoveCommentNode()
{
RemoveCommentNode(XmlDoc.DocumentElement);
}
#endregion
#region 其它交换节点/插入节点
/// <summary>
/// 两个节点交换位置
/// </summary>
/// <param name="XNodeFirst">第一个节点</param>
/// <param name="XNodeLast">第二个节点</param>
public void InterChange(XmlNode xNodeFirst, XmlNode xNodeLast)
{
if (xNodeFirst != null && xNodeLast != null)
{
if (xNodeFirst.ParentNode != null && xNodeLast.ParentNode != null)
{
xNodeFirst.ParentNode.ReplaceChild(xNodeLast.Clone(), xNodeFirst);
xNodeLast.ParentNode.ReplaceChild(xNodeFirst.Clone(), xNodeLast);
}
else
{
_XmlDocument.DocumentElement.ReplaceChild(xNodeLast.Clone(), xNodeFirst);
_XmlDocument.DocumentElement.ReplaceChild(xNodeFirst.Clone(), xNodeLast);
}
}
}
public void ReplaceNode(XmlNode newNode, string oldNodeIDorName)
{
ReplaceNode(newNode, Get(oldNodeIDorName));
}
/// <summary>
/// 节点替换[支持两个的文档间替换]
/// </summary>
/// <param name="NewXNode"></param>
/// <param name="OldXNode"></param>
public void ReplaceNode(XmlNode newNode, XmlNode oldNode)
{
if (newNode != null && oldNode != null)
{
if (newNode.Name == oldNode.Name) // 节点名相同。
{
oldNode.RemoveAll();//清空旧节点
oldNode.InnerXml = newNode.InnerXml;
XmlAttributeCollection attrs = newNode.Attributes;//设置属性
if (attrs != null && attrs.Count > 0)
{
for (int i = 0; i < attrs.Count; i++)
{
((XmlElement)oldNode).SetAttribute(attrs[i].Name, attrs[i].Value);
}
}
}
else
{
XmlNode xNode = CreateNode(newNode.Name, newNode.InnerXml);//先创建一个节点。
XmlAttributeCollection attrs = newNode.Attributes;
if (attrs != null && attrs.Count > 0)
{
for (int i = 0; i < attrs.Count; i++)
{
((XmlElement)xNode).SetAttribute(attrs[i].Name, attrs[i].Value);
}
}
oldNode.ParentNode.InsertAfter(xNode, oldNode);//挂在旧节点后面。
Remove(oldNode);
}
}
}
/// <summary>
/// 节点之后插入[支持两文档之间的插入]
/// </summary>
/// <param name="NewNode">要被插入的新节点</param>
/// <param name="RefNode">在此节点后插入NewNode节点</param>
public void InsertAfter(XmlNode newNode, XmlNode refNode)
{
XmlNode xDocNode = CreateNode(newNode.Name, "");
ReplaceNode(newNode, xDocNode);
refNode.ParentNode.InsertAfter(xDocNode, refNode);
}
/// <summary>
/// 节点之前插入[支持两文档之间的插入]
/// </summary>
/// <param name="NewNode">要被插入的新节点</param>
/// <param name="RefNode">在此节点前插入NewNode节点</param>
public void InsertBefore(XmlNode newNode, XmlNode refNode)
{
XmlNode xDocNode = CreateNode(newNode.Name, "");
ReplaceNode(newNode, xDocNode);
refNode.ParentNode.InsertBefore(xDocNode, refNode);
}
#endregion
#region 节点判断
public bool Contains(string idOrName)
{
return Get(idOrName) != null;
}
public bool Contains(string idOrName, XmlNode parentNode)
{
return Get(idOrName, parentNode) != null;
}
#endregion
#region 属性判断/取值
public bool HasAttr(string idOrName, string attrName)
{
return GetAttrValue(idOrName, attrName) != string.Empty;
}
public bool HasAttr(XmlNode node, string attrName)
{
return GetAttrValue(node, attrName) != string.Empty;
}
public string GetAttrValue(string idOrName, string attrName, params string[] defaultValue)
{
XmlNode node = Get(idOrName);
return GetAttrValue(node, attrName, defaultValue);
}
public string GetAttrValue(XmlNode node, string attrName, params string[] defaultValue)
{
if (node != null)
{
switch (attrName)
{
case "InnerText":
if (!string.IsNullOrEmpty(node.InnerText))
{
return node.InnerText;
}
break;
case "InnerXml":
if (!string.IsNullOrEmpty(node.InnerXml))
{
return node.InnerXml;
}
break;
default:
if (node.Attributes != null && node.Attributes[attrName] != null)
{
return node.Attributes[attrName].Value;
}
break;
}
}
if (defaultValue.Length > 0)
{
return defaultValue[0];
}
return string.Empty;
}
public void RemoveAttr(string idOrName, params string[] attrNames)
{
XmlNode node = Get(idOrName);
RemoveAttr(node, attrNames);
}
public void RemoveAttr(XmlNode node, params string[] attrNames)
{
if (node != null && node.Attributes != null)
{
foreach (string name in attrNames)
{
if (node.Attributes[name] != null)
{
node.Attributes.Remove(node.Attributes[name]);
}
}
}
}
#endregion
#region 操作数据
private bool _IsCurrentLang = true;
/// <summary>
/// 当前请求是否用户当前设置的语言
/// </summary>
public bool IsCurrentLang
{
get
{
return _IsCurrentLang;
}
set
{
_IsCurrentLang = value;
}
}
/// <summary>
/// 是否开始自定义语言分隔(分隔符号为:[#langsplit])
/// </summary>
public bool IsUseLangSplit = true;
private string SetValue(string sourceValue, string newValue, bool addCData)
{
if (string.IsNullOrEmpty(newValue))
{
return sourceValue;
}
newValue = newValue.Replace(ValueReplace.Source, sourceValue);
if (IsUseLangSplit)
{
int split = newValue.IndexOf(ValueReplace.LangSplit);
if (split > -1)
{
newValue = _IsCurrentLang ? newValue.Substring(0, split) : newValue.Substring(split + ValueReplace.LangSplit.Length);
}
}
if (addCData)
{
newValue = SetCDATA(newValue);
}
return newValue;
}
private void SetAttrValue(XmlNode node, string key, string value)
{
if (node == null || node.Attributes == null)
{
return;
}
if (node.Attributes[key] == null)
{
XmlAttribute attr = _XmlDocument.CreateAttribute(key);
node.Attributes.Append(attr);
}
value = SetValue(node.Attributes[key].InnerXml, value, false);
try
{
node.Attributes[key].Value = value;
}
catch
{
node.Attributes[key].Value = SetCDATA(value);
}
}
/// <summary>
/// 为节点赋值[通常值是在values中赋值]
/// </summary>
public void Set(XmlNode node, SetType setType, params string[] values)
{
if (node != null && values != null)
{
switch (setType)
{
case SetType.InnerText:
string value = SetValue(node.InnerText, values[0], false);
try
{
node.InnerText = value;
}
catch
{
node.InnerText = SetCDATA(value);
}
break;
case SetType.InnerXml:
node.InnerXml = SetValue(node.InnerXml, values[0], true);
break;
case SetType.Value:
case SetType.Href:
case SetType.Src:
case SetType.Class:
case SetType.Disabled:
case SetType.ID:
case SetType.Name:
case SetType.Visible:
case SetType.Title:
case SetType.Style:
string key = setType.ToString().ToLower();
SetAttrValue(node, key, values[0]);
break;
case SetType.Custom:
for (int i = 0; i < values.Length; i++)
{
if (i > 0 && i % 2 == 1)
{
key = values[i - 1].ToLower();
switch (key)
{
case "innertext":
Set(node, SetType.InnerText, values[i]);
break;
case "innerhtml":
case "innerxml":
Set(node, SetType.InnerXml, values[i]);
break;
default:
SetAttrValue(node, key, values[i]);
break;
}
}
}
break;
case SetType.A:
node.InnerXml = SetValue(node.InnerXml, values[0], true);
if (values.Length > 1)
{
SetAttrValue(node, "href", values[1]);
if (values.Length > 2)
{
SetAttrValue(node, "title", values[2]);
if (values.Length > 3)
{
SetAttrValue(node, "target", values[3]);
}
}
}
break;
case SetType.Select:
if (node.InnerXml.Contains(AppConfig.XHtml.CDataLeft))//带特殊字符
{
string innerHtml = node.InnerXml.Replace(string.Format("value=\"{0}\"", values[0]), string.Format("selected=\"selected\" value=\"{0}\"", values[0]));
try
{
node.InnerXml = innerHtml;
}
catch
{
node.InnerXml = SetCDATA(innerHtml);
}
}
else
{
foreach (XmlNode option in node.ChildNodes)
{
if (option.Attributes["value"] != null && (option.Attributes["value"].Value == values[0] || option.Attributes["value"].Value.Split(',')[0] == values[0]))
{
SetAttrValue(option, "selected", "selected");
break;
}
}
}
break;
case SetType.Checked:
if (node.Name == "input" && node.Attributes["type"].Value == "radio")
{
values[0] = "1";
}
switch (values[0].ToLower())
{
case "1":
case "true":
case "check":
case "checked":
key = setType.ToString().ToLower();
SetAttrValue(node, key, key);
break;
}
break;
}
}
}
public void Set(string idOrName, SetType setType, params string[] values)
{
Set(null, idOrName, setType, values);
}
public void Set(XmlNode parentNode, string idOrName, SetType setType, params string[] values)
{
XmlNode node = Get(idOrName, parentNode);
Set(node, setType, values);
}
public void Set(string idOrName, string value)
{
Set(null, idOrName, value);
}
/// <summary>
/// 对节点赋值(此方法会忽略hidden隐藏域,隐藏节点赋值请用其它重载方法)
/// </summary>
/// <param name="idOrName"></param>
/// <param name="value"></param>
public void Set(XmlNode parentNode, string idOrName, string value)
{
XmlNode node = Get(idOrName, parentNode);
if (node != null)
{
SetType setType = SetType.InnerXml;
switch (node.Name)
{
case "input":
switch (GetAttrValue(node, "type"))
{
case "hidden":
return;//此方法不对隐藏域处理。
case "checkbox":
setType = SetType.Checked; break;
case "image":
setType = SetType.Src; break;
case "radio"://情况复杂一点
XmlNodeList nodeList = GetList("input", "type", "radio");
for (int i = 0; i < nodeList.Count; i++)
{
if (GetAttrValue(nodeList[i], "name") == idOrName)
{
RemoveAttr(nodeList[i], "checked");
if (GetAttrValue(nodeList[i], "value") == value)
{
node = nodeList[i];
}
}
}
setType = SetType.Checked; break;
default:
setType = SetType.Value;
break;
}
break;
case "select":
setType = SetType.Select; break;
case "a":
setType = SetType.Href; break;
case "img":
setType = SetType.Src; break;
}
//try
//{
Set(node, setType, value);
//}
//catch (Exception err)
//{
// throw;
//}
}
}
#endregion
#region 重写最终输出OutXml
public override string OutXml
{
get
{
if (_XmlDocument != null)
{
#region 处理clearflag标签
string key = "clearflag";
XmlNodeList xnl = GetList("*", key);
if (xnl != null)
{
XmlNode xNode = null;
for (int i = xnl.Count - 1; i >= 0; i--)
{
xNode = xnl[i];
switch (GetAttrValue(xnl[i], key))
{
case "0":
RemoveAttr(xNode, key);
xNode.InnerXml = "";
break;
case "1":
Remove(xNode);
break;
}
}
}
#endregion
#region 处理XHtml 头前缀、清空CData
string xml = _XmlDocument.InnerXml.Replace(".dtd\"[]>", ".dtd\">");
if (xml.IndexOf(" xmlns=") > -1)
{
xml = xml.Replace(" xmlns=\"\"", string.Empty).Replace(" xmlns=\"" + xnm.LookupNamespace(PreXml) + "\"", string.Empty);
}
string html = ClearCDATA(xml);
if (!string.IsNullOrEmpty(docTypeHtml))
{
html = html.Replace(docTypeHtml, "<!DOCTYPE html>");
}
html = html.Replace(">", ">").Replace("<", "<").Replace("&", "&");//html标签符号。
#endregion
if (html.Contains("${")) // 替换自定义标签。
{
html = ReplaceCustomFlag(html, null, null);
}
return html;
}
return string.Empty;
}
}
private string ReplaceCustomFlag(string html, MDictionary<string, string> values, MDataRow row)
{
#region 替换自定义标签
MatchCollection matchs = Regex.Matches(html, @"\$\{([\S\s]*?)\}", RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (matchs != null && matchs.Count > 0)
{
List<string> keys = new List<string>(matchs.Count);
foreach (Match match in matchs)
{
//原始的占位符
string value = match.Groups[0].Value;//${txt#name:xx#xx},${'aaa'+txt#name:xx#xx+'bbb'}
if (keys.Contains(value))
{
continue;
}
keys.Add(value);
#region 分解占位符
//先处理+号 ${'aaa'+txt#name:xx#xx+'bbb'}
string formatter = "", key = "";
foreach (string item in match.Groups[1].Value.Trim().Split('+'))
{
if (item[0] == '"')
{
formatter += item.Trim('"');
}
else if (item[0] == '\'')
{
formatter += item.Trim('\'');
}
else
{
if (!string.IsNullOrEmpty(key))
{
break;//只允许存在一个key : txt#name:这是描述说明。
}
formatter += "{0}";
key = item.Trim();
}
}
string columnName = key.Split(':')[0];//name
#endregion
string replaceValue = "";
#region 获取占位符的值
if (value != null && row != null)
{
replaceValue = GetValueByTable(columnName, key, values, row);
}
if (string.IsNullOrEmpty(replaceValue))
{
replaceValue = GetValueByKeyValue(columnName);
}
if (string.IsNullOrEmpty(replaceValue))
{
replaceValue = GetValueByRequest(columnName, key);
}
#endregion
html = html.Replace(value, string.IsNullOrEmpty(replaceValue) ? "" : string.Format(formatter, replaceValue));
}
keys.Clear();
keys = null;
matchs = null;
}
#endregion
return html;
}
private string GetValueByKeyValue(string columnName)
{
//if (PreList != null && PreList.Contains(pre) && _Row != null)
//{
// MDataCell matchCell = _Row[columnName];
// if (matchCell != null)
// {
// return matchCell.ToString();
// }
//}
//处理keyValue替换
if (KeyValue.Count > 0 && KeyValue.ContainsKey(columnName))
{
return KeyValue[columnName];
}
return string.Empty;
}
private string GetValueByTable(string columnName, string key, MDictionary<string, string> values, MDataRow row)
{
if (values.ContainsKey(columnName))//值可能被格式化过,所以优先取值。
{
return values[columnName];
}
else
{
MDataCell matchCell = row[columnName];
if (matchCell != null)
{
return matchCell.ToString();
}
}
return string.Empty;
}
private string GetValueByRequest(string columnName, string key)
{
string replaceValue = string.Empty;
if (HttpContext.Current != null)
{
replaceValue = HttpContext.Current.Request[columnName] ?? HttpContext.Current.Request.Headers[columnName];
}
if (string.IsNullOrEmpty(replaceValue) && key.Contains(":"))//如果为空,设置默认的说明。
{
replaceValue = key.Substring(key.LastIndexOf(':') + 1);
}
return replaceValue;
}
#endregion
}
//扩展交互
public partial class XHtmlAction
{
#region 操作数据
MDataRow _Row;
MDataTable _Table;
#region 加载表格循环方式
/// <summary>
/// 加载MDataTable的多行数据
/// </summary>
public void LoadData(object anyObjToTable)
{
LoadData(MDataTable.CreateFrom(anyObjToTable));
}
/// <summary>
/// 装载数据行 (一般后续配合SetForeach方法使用)
/// </summary>
public void LoadData(MDataTable table)
{
_Table = table;
if (_Table.Rows.Count > 0)
{
_Row = _Table.Rows[0];
}
}
public delegate string SetForeachEventHandler(string text, MDictionary<string, string> values, int rowIndex);
/// <summary>
/// 对于SetForeach函数调用的格式化事件
/// </summary>
public event SetForeachEventHandler OnForeach;
public void SetForeach()
{
if (_Table != null)
{
XmlNode node = Get(_Table.TableName + "View");
if (node == null)
{
node = Get("defaultView");
}
if (node != null)
{
SetForeach(node, node.InnerXml);
}
}
}
public void SetForeach(string idOrName, SetType setType, params object[] formatValues)