从给定的xml文件中抽取信息,经过处理后,产生新的xml文件

工作上需要测试n台服务器部署后的配置信息是否正确,用既有的工具进行测试,工具的执行需要一个xml参数文件,当机器数目少的时候,手工去编写这个xml文件就可以了,出错的几率也不会很大,但是当机器数目达到几十、几百甚至更多的时候,就有必要用程序生成了,这样高效而准确。

思路如下:

准备输入的源xml文件和输出的xml文件模板;

从原文件中抽取服务器类型和机器名存入List中;

根据服务器类型拼出对应的xml节点,对输出的xml文件模板的部分内容作替换;

代码如下:

 

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace CreateXMLsForMPAP
{
class Program
{
private const string machinelist_mp = "Machinelist_MP.xml";
private const string machinelist_ap = "Machinelist_AP.xml";
private const string template = "VerificationTemplate.xml";
private const string config_value_AP = @"output\config_value_AP.xml";
private const string config_value_MP = @"output\config_value_MP.xml";
static void Main(string[] args)
{
CreateXml(PilotType.MP);
CreateXml(PilotType.AP);
}

public static void CreateXml(PilotType pilotType)
{
string path = string.Empty;
//Path.Combine(Directory.GetCurrentDirectory(), config_value_MP);
if (pilotType == PilotType.MP) path = config_value_MP;
else path = config_value_AP;
File.Copy(template, path,
true);

StringBuilder sb
= new StringBuilder();

string sourcepath = string.Empty;
if (pilotType == PilotType.MP)
{
sourcepath
= "\\D$\\IDSS\\AC\\Data\\DeliveryEngine\\";
SetVerificationNodeForMP(sb, sourcepath);
}
else
{
sourcepath
= "\\D$\\Data\\DeliveryEngine\\";
SetVerificationNodeForAP(sb, sourcepath);
}

#region replace
StreamReader sr
= new StreamReader(path);
string str;
str
= sr.ReadToEnd();
sr.Close();

str
= str.Replace("$verifications$", sb.ToString());

StreamWriter sw
= new StreamWriter(path);
sw.Write(str);
sw.Flush();
sw.Close();
#endregion
}

private static void SetVerificationNodeForMP(StringBuilder sb, string sourcepath)
{
List
<string> machineList;
machineList
= getMachineList(ServiceType.Keyword, PilotType.MP);
foreach (string machine in machineList)
{
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\Keyword\\KWConfig.xml", "/KWEngine/KWEtoQRDT/TimeoutThreshold", "1000");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\Keyword\\KWConfig.xml", "/KWEngine/KWEtoQRDT/LatencyStops", "999");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\Keyword\\KWConfig.xml", "/KWEngine/KWEtoQRDT/LatencyBucketTolerances", "10000,9999");

setVerificationNode(sb, machine, sourcepath
+ "Config\\Dynamic\\Keyword\\KWDynamicConfig.xml", "/KWEngineDynamicConfig/KWEtoQRDT/GetWordbagsfromQRDT", "0");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Dynamic\\Keyword\\KWDynamicConfig.xml", "/KWEngineDynamicConfig/KWEtoQRDT/AsyncQRDTLegacyOn", "0");
}

machineList
= getMachineList(ServiceType.Listing, PilotType.MP);
foreach (string machine in machineList)
{
//can ignore this
setVerificationNode(sb, machine, sourcepath + "Config\\Static\\Listing\\ListingConfig.xml", "/ListingEngine/OnlineRV/ListingLADTMessageType", "0");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\Listing\\ListingConfig.xml", "/ListingEngine/OnlineRV/LADTPartitionKeyType", "0");

setVerificationNode(sb, machine, sourcepath
+ "Config\\Dynamic\\Listing\\ListingDynamicConfig.xml", "/ListingDynamicConfig/OnlineRV/MaxCrawlBatch", "768");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Dynamic\\Listing\\ListingDynamicConfig.xml", "/ListingDynamicConfig/OnlineRV/ConnectToQRDTStore", "1");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Dynamic\\Listing\\ListingDynamicConfig.xml", "/ListingDynamicConfig/OnlineRV/ConnectToLADTStore", "0");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Dynamic\\Listing\\ListingDynamicConfig.xml", "/ListingDynamicConfig/OnlineRV/ConnectToSearchStore", "1");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Dynamic\\Listing\\ListingDynamicConfig.xml", "/ListingDynamicConfig/OnlineRV/ConnectToCrawlStore", "1");

//Listing-OLS-OLSConfig.xml(static) there are lots of <datastore> node
//setVerificationNode(sb, machine, sourcepath + "Config\\Static\\Listing\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore", "");
}
machineList
= getMachineList(ServiceType.QRDT, PilotType.MP);
foreach (string machine in machineList)
{
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\QRDT\\QRDTConfig.xml", "/QRDTConfig/IgnoreDCVIPMismatch", "0");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\QRDT\\QRDTConfig.xml", "/QRDTConfig/ReturnBeforeSearch", "0");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\QRDT\\QRDTConfig.xml", "/QRDTConfig/ContactSQM", "0");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\QRDT\\QRDTConfig.xml", "/QRDTConfig/UpdateAllReplicas", "0");

//need to change
setVerificationNode(sb, machine, sourcepath + "Config\\Static\\QRDT\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/PhysicalStore/IncludeExternalMachines", "false");
}

machineList
= getMachineList(ServiceType.LADT, PilotType.MP);
foreach (string machine in machineList)
{
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\LADT\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/Key/Name", "URLHashABTestID");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\LADT\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/Key/Type", "Binary");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\LADT\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/Key/Size", "12");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\LADT\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/PhysicalStore/Compression/Enabled", "false");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\LADT\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/PhysicalStore/Compression/BlockSize", "4096");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\LADT\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/PhysicalStore/Compression/CompressionLevel", "5");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\LADT\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/PhysicalStore/Compression/CacheSizePercent", "15");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\LADT\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/PhysicalStore/Compression/NumberThreads", "7");
}

machineList
= getMachineList(ServiceType.CQM, PilotType.MP);
foreach (string machine in machineList)
{
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\CrawlQueue\\QueueManagerConfig.xml", "/OLSQueueManagerConfig/ListingLADTMessageType", "0");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\CrawlQueue\\QueueManagerConfig.xml", "/OLSQueueManagerConfig/LADTPartitionKeyType", "0");
//CQM-OLS-OLSConfig.xml(static) there are lots of <datastore> node
setVerificationNode(sb, machine, sourcepath + "Config\\Static\\CrawlQueue\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/Key/Name", "URLHashABTestID");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\CrawlQueue\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/Key/Type", "Binary");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\CrawlQueue\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/Key/Size", "12");
}
}
private static void SetVerificationNodeForAP(StringBuilder sb, string sourcepath)
{
List
<string> machineList;
machineList
= getMachineList(ServiceType.Keyword, PilotType.AP);
foreach (string machine in machineList)
{
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\Keyword\\KWConfig.xml", "/KWEngine/KWEtoQRDT/TimeoutThreshold", "20");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\Keyword\\KWConfig.xml", "/KWEngine/KWEtoQRDT/LatencyStops", "19");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\Keyword\\KWConfig.xml", "/KWEngine/KWEtoQRDT/LatencyBucketTolerances", "10000,10000");

setVerificationNode(sb, machine, sourcepath
+ "Config\\Dynamic\\Keyword\\KWDynamicConfig.xml", "/KWEngineDynamicConfig/KWEtoQRDT/GetWordbagsfromQRDT", "1");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Dynamic\\Keyword\\KWDynamicConfig.xml", "/KWEngineDynamicConfig/KWEtoQRDT/AsyncQRDTLegacyOn", "0");
}

machineList
= getMachineList(ServiceType.Listing, PilotType.AP);
foreach (string machine in machineList)
{
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\Listing\\ListingConfig.xml", "/ListingEngine/OnlineRV/ListingLADTMessageType", "1");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\Listing\\ListingConfig.xml", "/ListingEngine/OnlineRV/LADTPartitionKeyType", "1");

setVerificationNode(sb, machine, sourcepath
+ "Config\\Dynamic\\Listing\\ListingDynamicConfig.xml", "/ListingDynamicConfig/OnlineRV/MaxCrawlBatch", "2048");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Dynamic\\Listing\\ListingDynamicConfig.xml", "/ListingDynamicConfig/OnlineRV/ConnectToQRDTStore", "0");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Dynamic\\Listing\\ListingDynamicConfig.xml", "/ListingDynamicConfig/OnlineRV/ConnectToLADTStore", "1");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Dynamic\\Listing\\ListingDynamicConfig.xml", "/ListingDynamicConfig/OnlineRV/ConnectToSearchStore", "0");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Dynamic\\Listing\\ListingDynamicConfig.xml", "/ListingDynamicConfig/OnlineRV/ConnectToCrawlStore", "1");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Dynamic\\Listing\\ListingDynamicConfig.xml", "/ListingDynamicConfig/OnlineRV/MaxPagesToCrawl", "2");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Dynamic\\Listing\\ListingDynamicConfig.xml", "/ListingDynamicConfig/OnlineRV/PrioritizePageCrawl", "1");

//Listing-OLS-OLSConfig.xml(static) there are lots of <datastore> node
//setVerificationNode(sb, machine, sourcepath + "Config\\Static\\Listing\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore", "");
}
machineList
= getMachineList(ServiceType.QRDT, PilotType.AP);
foreach (string machine in machineList)
{
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\QRDT\\QRDTConfig.xml", "/QRDTConfig/IgnoreDCVIPMismatch", "1");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\QRDT\\QRDTConfig.xml", "/QRDTConfig/ReturnBeforeSearch", "1");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\QRDT\\QRDTConfig.xml", "/QRDTConfig/ContactSQM", "1");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\QRDT\\QRDTConfig.xml", "/QRDTConfig/UpdateAllReplicas", "1");

//need to change
setVerificationNode(sb, machine, sourcepath + "Config\\Static\\QRDT\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/Flags/ReplicationModel", "SyncOnRestart");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\QRDT\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/PhysicalStore/IncludeExternalMachines", "true");
}

machineList
= getMachineList(ServiceType.LADT, PilotType.AP);
foreach (string machine in machineList)
{
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\LADT\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/Persistence/LoadAllRecords", "true");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\LADT\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/Key/Name", "URLHashCampaignIDABTestID");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\LADT\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/Key/Type", "Binary");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\LADT\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/Key/Size", "16");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\LADT\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/Flags/ReplicationModel", "SyncOnRestart");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\LADT\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/Flags/UserDefinedPartition", "true");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\LADT\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/PhysicalStore/Compression/Enabled", "true");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\LADT\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/PhysicalStore/Compression/BlockSize", "24576");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\LADT\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/PhysicalStore/Compression/CompressionLevel", "9");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\LADT\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/PhysicalStore/Compression/CacheSizePercent", "0");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\LADT\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/PhysicalStore/Compression/NumberThreads", "7");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\LADT\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/PhysicalStore/Compression/CompressionBufferSize", "50");
}

machineList
= getMachineList(ServiceType.CQM, PilotType.AP);
foreach (string machine in machineList)
{
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\CrawlQueue\\QueueManagerConfig.xml", "/OLSQueueManagerConfig/ListingLADTMessageType", "1");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\CrawlQueue\\QueueManagerConfig.xml", "/OLSQueueManagerConfig/LADTPartitionKeyType", "1");
//CQM-OLS-OLSConfig.xml(static) there are lots of <datastore> node
setVerificationNode(sb, machine, sourcepath + "Config\\Static\\CrawlQueue\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/Key/Name", "URLHashCampaignIDABTestID");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\CrawlQueue\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/Key/Type", "Binary");
setVerificationNode(sb, machine, sourcepath
+ "Config\\Static\\CrawlQueue\\OlsConfig.xml", "/OLSConfiguration/DataStores/DataStore/Key/Size", "16");
}
}

private static StringBuilder setVerificationNode(StringBuilder sb, string machine,string sourcefile,string nodepath,string value)
{
sb.Append(
"<Verification Type=\"ConfigurationValueVerification\" Mode=\"IgnoreCase\" Severity=\"Error\" Description=\"\">");
sb.Append(
"<SourceFile>");
sb.Append(
"\\\\").Append(machine).Append(sourcefile);
sb.Append(
"</SourceFile>");
sb.Append(
"<NodePath>");
sb.Append(nodepath);
sb.Append(
"</NodePath>");
sb.Append(
"<Value>");
sb.Append(value);
sb.Append(
"</Value>");
sb.Append(
"</Verification>");
return sb;
}

#region get machine list on ORV
/// <summary>
/// get machinelist according to service type
/// </summary>
/// <param name="serviceType"></param>
/// <param name="pilotType"></param>
/// <returns></returns>
public static List<string> getMachineList(ServiceType serviceType, PilotType pilotType)
{
List
<string> machineList = new List<string>();
List
<string> machines = new List<string>();
machineList
= getMachineList(pilotType);
foreach (string item in machineList)
{
if (item.StartsWith(serviceType.ToString()))
machines.Add(item.Split(
',')[1]);
}
return machines;
}
/// <summary>
/// get all machinelist(service,machinename)
/// </summary>
/// <param name="pilotType"></param>
/// <returns></returns>
public static List<string> getMachineList(PilotType pilotType)
{
List
<string> machineList = new List<string>();
string machinelistxml = string.Empty;
XmlDocument xml
= new XmlDocument();
if (pilotType == PilotType.MP)
machinelistxml
= machinelist_mp;
else
machinelistxml
= machinelist_ap;
xml.Load(machinelistxml);
XmlNodeList node
= xml.SelectNodes("//MachineList/Environment/Engine");
bool exist = false;
foreach (XmlNode item in node)
{
if (item.Attributes["Type"].Value == "PaidSearch")
{
XmlNodeList clusterList
= item.SelectNodes("Cluster/Service");

foreach (XmlNode itemService in clusterList)
{
XmlNodeList machinesList
= itemService.SelectNodes("MachineGroup/Machine");
foreach (XmlNode itemMachine in machinesList)
{
foreach (XmlAttribute attri in itemMachine.Attributes)//exclude type=reference
{
if (attri.Name == "Type") exist = true;
}
if (!exist)
{
XmlNodeList machines
= itemMachine.SelectNodes("Name");
foreach (XmlNode itemMachineName in machines)
{
machineList.Add(itemService.Attributes[
"Name"].Value + "," + itemMachineName.InnerText);
}
}
exist
= false;
}
}
}
}
return machineList;
}
#endregion
}

#region Define two enum for Service and Pilot type
/// <summary>
/// Service type on ORV, including Keyword,Listing,QRDT,LADT,CQM
/// </summary>
public enum ServiceType
{
Keyword
= 0,
Listing
= 1,
QRDT
= 2,
LADT
= 3,
CQM
= 4
};

/// <summary>
/// Pilot type, including Manual Pilot(MP) and Aotomatic Pilot(AP)
/// </summary>
public enum PilotType
{
MP
= 0,
AP
= 1
};
#endregion
}

 

 

最后,将所需要的输入文件输出到输出目录,这里的输出目录是bin/debug目录,具体操作如下:

右击项目名称,点击“Properties”,选择“Build Events”,点击“Edit Post-build...”,在commandline中键入

md $(TargetDir)output
copy $(ProjectDir)*.xml  $(TargetDir)

当然也可以点击“Macros”进行选择。

类似于$(TargetDir)的意义,可以参见http://msdn.microsoft.com/en-us/library/42x5kfw4(VS.80).aspx

posted on 2010-06-01 14:56  -Anny-  阅读(423)  评论(0编辑  收藏  举报