public static XmlDocument AppendSingleNode(XmlIndex index, XmlNode sourcenode, XmlDocument target) { Console.WriteLine("Do Cut!"); XmlNode ParentNode = target.SelectSingleNode(Config.XmlParentElementName); Console.WriteLine("Cut:" + sourcenode); ParentNode.AppendChild(target.ImportNode(sourcenode, true)); return(target); }
public static void Action() { if (BuildDirectory()) { Console.WriteLine("Created target directory:" + TargetDirectory.FullName); } else { Program.Termination(-1, "Target directory create error:" + TargetDirectory.FullName); } if (BuildMetaData()) { Console.WriteLine("Finished MetaData Building."); } else { Program.Termination(-1, "MetaData Building Error!"); } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Start Xml Cutter!"); for (int x = 0; x < Config.XmlIndexList.Count; x++) { XmlIndex Index = Config.XmlIndexList[x]; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("--------------------"); Console.WriteLine("[{0}/{1}]Cut <{2}>", x + 1, Config.XmlIndexList.Count, Index.Root); Console.ForegroundColor = ConsoleColor.Green; if (!string.IsNullOrEmpty(Index.Type)) { XmlSet = CreateXml(); Console.WriteLine("Create xmlset with <{0}>", Index.Type); NeedSave = true; } else { NeedSave = false; } for (int i = 0; i < Config.XmlFileList.Count; i++) { FileInfo File = Config.XmlFileList[i]; Console.WriteLine("===================="); Console.WriteLine("[{0}/{1}]Cut {2}", i + 1, Config.XmlFileList.Count, File.FullName); Console.Title = Config.ConsoleTitle + String.Format(" Cooking [{0}/{1}]-Step:<{2}> [{3}/{4}]-File:{5}", x + 1, Config.XmlIndexList.Count, Index.Root, i + 1, Config.XmlFileList.Count, File.Name); if (Do(Index, File)) { Console.WriteLine("{0} {1}", File.FullName, "done!"); } else { Program.Error(File.FullName + " not finished!"); Console.ForegroundColor = ConsoleColor.Green; } } if (NeedSave) { string Path = TargetDirectory + @"\" + Index.Root + @"\" + Index.Type + ".xml"; Console.WriteLine("Xml Save As:" + Path); XmlSet.Save(Path); NeedSave = false; } } Console.ForegroundColor = ConsoleColor.Gray; }
public static bool Do(XmlIndex index, FileInfo file) { try { if (string.IsNullOrEmpty(index.Type)) { Console.WriteLine("No type, this is single xml, file name as:" + index.Attr); XmlDocument SourceXml = new XmlDocument(); SourceXml.Load(file.FullName); System.Console.WriteLine("Load Source:" + file.FullName); XmlDocument TargetXml; System.Console.WriteLine("Created Target Document!"); Console.WriteLine("Try Cut!"); XmlNodeList CutNodeList = SourceXml.GetElementsByTagName(index.Root); Console.WriteLine("Cut Node List Size:" + CutNodeList.Count); foreach (XmlNode node in CutNodeList) { Console.WriteLine("Try Append Single Xml!"); TargetXml = AppendXmlElementFromList(SourceXml, CreateXml()); Console.WriteLine("Xml Append Finished!"); Console.WriteLine("Try Cut Single Xml!"); XmlDocument SingleXml = AppendSingleNode(index, node, TargetXml); Console.WriteLine("Xml Cut Finished!"); string Path = TargetDirectory + @"\" + index.Root + @"\" + ((XmlElement)node).GetAttribute(index.Attr) + ".xml"; Console.WriteLine("Xml Save As:" + Path); SingleXml.Save(Path); Console.WriteLine("Done!"); } } else { Console.WriteLine("Get type, save as xml set, file name as:" + index.Type); XmlDocument SourceXml = new XmlDocument(); SourceXml.Load(file.FullName); XmlNode ParentNode = XmlSet.SelectSingleNode(Config.XmlParentElementName); XmlNodeList SourceNode = SourceXml.GetElementsByTagName(index.Root); Console.WriteLine("Cut SourceNode <{0}>, target size:{1}.", index.Root, SourceNode.Count); if (SourceNode.Count > 0) { Console.WriteLine("Try Append Xml!"); Console.WriteLine("Do Append!"); foreach (string s in Config.XmlElementAppendList) { XmlNodeList SourceAppendNodeList = SourceXml.GetElementsByTagName(s); Console.WriteLine("Target Node Name:" + Config.XmlParentElementName); XmlNode TargetAppendNode = XmlSet.SelectSingleNode(Config.XmlParentElementName); Console.WriteLine("Get {0} <{1}>, merge into target <{2}>.", SourceAppendNodeList.Count, s, TargetAppendNode.Name); foreach (XmlNode i in SourceAppendNodeList) { XmlNode Node = i.CloneNode(true); Console.WriteLine("Append:" + Node.OuterXml); TargetAppendNode.AppendChild(XmlSet.ImportNode(Node, true)); } } Console.WriteLine("Xml Append Finished!"); Console.WriteLine("Try Cut Xml!"); Console.WriteLine("Do Cut!"); foreach (XmlNode i in SourceNode) { Console.WriteLine("Cut:" + i.OuterXml); ParentNode.AppendChild(XmlSet.ImportNode(i, true)); } Console.WriteLine("Cut Done!"); } else { Console.WriteLine("No target node, pass."); } } } catch (Exception e) { Program.Error(e.ToString()); return(false); } return(true); }
public static bool LoadConfig() { Console.Write("Try to get config file:" + ConfigPath + " Result:"); if (File.Exists(ConfigPath)) { Console.WriteLine("Is exists!"); } else { Console.WriteLine("Is not exists! "); Program.Termination(-1, ConfigPath + " Is not exists! Please create a config!"); } XmlDocument ConfigDocument = new XmlDocument(); ConfigDocument.Load(ConfigPath); XmlNode IndexListNode = ConfigDocument.SelectSingleNode("Config/IndexList"); if (IndexListNode == null) { Program.Termination(-1, ConfigPath + " doesn't have Config/IndexList!"); } XmlNodeList XmlIndexList = IndexListNode.SelectNodes("XmlIndex"); if (XmlIndexList.Count < 0) { Program.Termination(-1, ConfigPath + " doesn't have any XmlIndex data!"); } System.Console.WriteLine("Read Parent Data..."); try { XmlNode ParentNode = ConfigDocument.SelectSingleNode("Config/Parent"); XmlElement ParentElement = (XmlElement)ParentNode; XmlParentElementName = ParentElement.GetAttribute("element"); System.Console.WriteLine("Find parent element:" + XmlParentElementName); foreach (XmlElement e in ParentElement.SelectSingleNode("attr").ChildNodes) { System.Console.WriteLine("Find parent attr:" + e.Name + "|" + e.GetAttribute("attr")); XmlParentElementAttr.Add(e.Name, e.GetAttribute("attr")); } } catch (Exception e) { Program.Error("Error Parent Data:" + e.Message); Program.Error("Set Parent Data NULL!"); XmlParentElementName = null; XmlParentElementAttr = null; } System.Console.WriteLine("Read Append Data..."); try { XmlNode ParentNode = ConfigDocument.SelectSingleNode("Config/Append"); XmlElement ParentElement = (XmlElement)ParentNode; foreach (XmlElement e in ParentElement.ChildNodes) { System.Console.WriteLine("Find append Element:" + e.Name); Config.XmlElementAppendList.Add(e.Name); } } catch (Exception e) { Program.Error("Error Append Data:" + e.Message); Program.Error("Set Append Data NULL!"); XmlElementAppendList = null; } if (XmlIndexList.Count <= 0) { Program.Termination(-1, " No XmlIndexs in config."); } foreach (XmlNode i in XmlIndexList) { XmlIndex Node = new XmlIndex(); XmlElement iElement = (XmlElement)i; Node.Root = iElement.GetAttribute("root"); Node.Attr = iElement.GetAttribute("attr"); Node.Type = iElement.GetAttribute("type"); Console.WriteLine("New index loaded:" + Node.ToString()); if (Node.Root.Length <= 0) { Program.Error("Where are the Root?:" + Node.ToString()); } else { Config.XmlIndexList.Add(Node); } } return(true); }