占座
注释:该程序主要用于根据模板,提取XML中相应数据
1 public static List<XElement> textToExtract = new List<XElement>(); 2 3 public void test(XElement sample, XElement template) 4 { 5 XAttribute ExtractionTag = new XAttribute("shouldextract", "true"); 6 var requiredAttributes = template.Attributes().ToList<XAttribute>(); 7 var sampleAttributes = sample.Attributes().ToList<XAttribute>(); 8 9 if (0 == requiredAttributes.Count) 10 { 11 foreach (var templateItem in template.Elements()) 12 { 13 foreach (var sampleItem in sample.Elements()) 14 { 15 if (templateItem.Name.LocalName == sampleItem.Name.LocalName) 16 { 17 test(sampleItem, templateItem); 18 } 19 } 20 } 21 } 22 else 23 { 24 if (Contains(requiredAttributes, ExtractionTag)) 25 { 26 XAttribute tagToRemove = null; 27 foreach (var item in requiredAttributes) 28 { 29 if (item.Name.LocalName.Equals(ExtractionTag.Name.LocalName) && 30 item.Value.Equals(ExtractionTag.Value)) 31 tagToRemove = item; 32 } 33 requiredAttributes.Remove(tagToRemove); 34 if (0 == requiredAttributes.Count) 35 { 36 textToExtract.Add(sample); 37 } 38 else if (Contains(sampleAttributes, requiredAttributes)) 39 { 40 textToExtract.Add(sample); 41 } 42 } 43 else 44 { 45 foreach (var templateItem in template.Elements()) 46 { 47 foreach (var sampleItem in sample.Elements()) 48 { 49 if (templateItem.Name.LocalName == sampleItem.Name.LocalName) 50 { 51 test(sampleItem, templateItem); 52 53 } 54 } 55 } 56 } 57 } 58 } 59 60 bool Contains(List<XAttribute> container, List<XAttribute> subToTest) 61 { 62 foreach (var item in subToTest) 63 { 64 var flag = Contains(container, item); 65 if (!flag) 66 { 67 return false; 68 } 69 } 70 return true; 71 } 72 bool Contains(List<XAttribute> container, XAttribute subToTest) 73 { 74 foreach (var item in container) 75 { 76 if (item.Name.LocalName.Equals(subToTest.Name.LocalName) && 77 item.Value.Equals(subToTest.Value)) 78 { 79 return true; 80 } 81 } 82 return false; 83 }
占座