1.通过递归获取文件夹下的所有xml文件,并提取xml中内容
xml格式如下
<?xml version="1.0" encoding="utf-8"?> <ROOT> <HEADDATA> <FROMPLATFORMCODE>E5119003117</FROMPLATFORMCODE> <TOPLATFORMCODE>P540</TOPLATFORMCODE> <DATA_NO>50</DATA_NO> <DATA_EN>PUECT</DATA_EN> <DATA_CN>目</DATA_CN> <DATA_TYPE>1</DATA_TYPE> <DATA_KEY>24506689-938f-4517a6</DATA_KEY> <TIMESTAMP>20190801084902</TIMESTAMP> <VERSION>2.0</VERSION> </HEADDATA> <DESCONTEXT> <PROGRAM_BUDGET>188000.000000</PROGRAM_BUDGET> <RELATE_CODE>0</RELATE_CODE> <PURCHASE_PROJECT_CODE>巴中竞采【2019】279-1号</PURCHASE_PROJECT_CODE> <PURCHASE_PROJECT_NAME>计算机(台式电脑)、服务器、交换设备、其他办公用品采购</PURCHASE_PROJECT_NAME> <UNIFIED_DEAL_CODE>D01-12513700062370904X-801-033087-T</UNIFIED_DEAL_CODE> <INVEST_PROJECT_CODE /> <TENDER_PROJECT_TYPE>D01</TENDER_PROJECT_TYPE> </DESCONTEXT> <DESBINARYS /> </ROOT>
需要查找的路径
List<keyValue> diclist = new List<keyValue>();
string path = @"E:\网站发布\ToprovinceFile\File"; getDirectory(path);
public void getDirectory(string path) { getFileName(path); DirectoryInfo root = new DirectoryInfo(path); foreach (DirectoryInfo d in root.GetDirectories()) { getDirectory(d.FullName); } }
public void getFileName(string path) { DirectoryInfo root = new DirectoryInfo(path); foreach (FileInfo item in root.GetFiles()) { if (item.Name.Contains("550.xml")) { XmlDataDocument fileDoc = new XmlDataDocument(); fileDoc.Load(Common.MapPath(item.FullName)); var fileNode = fileDoc.SelectSingleNode("/ROOT");//获取根目录 var str = fileNode.SelectSingleNode("HEADDATA/DATA_KEY").InnerText;//获取note节点内容 var key = fileNode.SelectSingleNode("DESCONTEXT/UNIFIED_DEAL_CODE").InnerText; //<UNIFIED_DEAL_CODE>D01-12513700062370904X-20180423-026568-E</UNIFIED_DEAL_CODE> diclist.Add(new keyValue() { key = key, vallue = str }); } } }
2.string格式的xml文件 提取其中部分内容
stringxml格式
<?xml version="1.0"?> <DESCONTEXT> <PROGRAM_BUDGET>395000.000000</PROGRAM_BUDGET> <RELATE_CODE>0</RELATE_CODE> <PRICE_FORM_CODE>1</PRICE_FORM_CODE> <OTHER_PROGRAM_BUDGET>395000</OTHER_PROGRAM_BUDGE> <PLATFORM_CODE>12513700062370904X</PLATFORM_CODE> <PUB_SERVICE_PLAT_CODE>12513700062370904X</PUB_SERVICE_PLAT_CODE> <DATA_TIMESTAMP>20190520085121</DATA_TIMESTAMP> <REGION_CODE>511900</REGION_CODE> <PURCHASE_PROJECT_CODE>巴中竞采【2019】172-1号</PURCHASE_PROJECT_CODE> <PURCHASE_PROJECT_NAME>计算机(台式电脑)、投影仪、交换设备、其他办公用品采购</PURCHASE_PROJECT_NAME> <UNIFIED_DEAL_CODE>D01-12513700062370904X-20190520-032086-F</UNIFIED_DEAL_CODE> <INVEST_PROJECT_CODE/> <TENDER_PROJECT_TYPE>D01</TENDER_PROJECT_TYPE> </DESCONTEXT>
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml("以上示例xml");
var fileNode = xdoc.SelectSingleNode("/DESCONTEXT");
var str = fileNode.SelectSingleNode("UNIFIED_DEAL_CODE").InnerText;