本文介绍了基于元素计数拆分XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 您好, i有一个大的XML文件,我希望根据元素计数分成几个文件 Ex。如果文件如下所示 Hello,i have a big XML file,which i want to split in several files based on Element countEx. if the file is like below <bookstore> <book category="..."> <title >...</title> <author>...</author> <year>...</year> <price>...</price> </book> <book category="..."> <title >...</title> <author>...</author> <year>...</year> <price>...</price> </book> <book category="..."> <title >...</title> <author>...</author> <year>...</year> <price>...</price> </book> <book category="..."> <title >...</title> <author>...</author> <year>...</year> <price>...</price> </book></bookstore> 每个文件只应包含2本书因此它应生成两个文件,如下所示 each file should contain 2 books only Therefore it should generate two files like below<bookstore> <book category="..."> <title >...</title> <author>...</author> <year>...</year> <price>...</price> </book> <book category="..."> <title >...</title> <author>...</author> <year>...</year> <price>...</price> </book></bookstore> 有什么想法吗?Any ideas?推荐答案 //New xml document object XmlDocument newXmlDoc = new XmlDocument(); //XML writer to be used XmlTextWriter Xwr ;//Read Root Element from XML XmlElement root = Doc.DocumentElement; //node nae to load nodes list if (Doc.DocumentElement.ChildNodes.Count > 0) RootNodeName = "//" + root.Name + "/" + Doc.DocumentElement.ChildNodes[0].Name; else RootNodeName = "//" + root.Name; //load all client noded in Node List XmlNodeList NodeList = Doc.SelectNodes(RootNodeName); //crete new doc and write empty root node XmlNode CurrentNode = null; XmlNode RootNode = newXmlDoc.CreateElement(root.Name); newXmlDoc.AppendChild(RootNode); for (int i = 0; i < NodeList.Count; i++) { CurrentNode = NodeList[i]; //to skip empty nodes if (CurrentNode.InnerXml == null || CurrentNode.InnerXml.Trim() == "") continue; //append nodes till batch size is reached if (RecCount < iBatchSize) { XmlNode targetNode = newXmlDoc.ImportNode(CurrentNode, true); RootNode.AppendChild(targetNode); RecCount = RecCount + 1; } else { //once batch size is reached, save file and re-initialise xml document FileCount = FileCount + 1; Xwr = new XmlTextWriter(FilePath + @"\" + FileName + "_" + FileCount.ToString() + ".xml", Encoding.UTF8); Xwr.Formatting = Formatting.None; newXmlDoc.Save(Xwr); Xwr.Close(); Xwr = null; newXmlDoc = null; newXmlDoc = new XmlDocument(); RootNode = newXmlDoc.CreateElement(root.Name); newXmlDoc.AppendChild(RootNode); XmlNode targetNode = newXmlDoc.ImportNode(CurrentNode, true); RootNode.AppendChild(targetNode); RecCount = 1; } if (i == NodeList.Count - 1) { if (RecCount > 0) { //if remaining items are less than Batchsize, save file at last FileCount = FileCount + 1; Xwr = new XmlTextWriter(FilePath + @"\" + FileName + "_" + FileCount.ToString() + ".xml", Encoding.UTF8); Xwr.Formatting = Formatting.None; newXmlDoc.Save(Xwr); Xwr.Close(); Xwr = null; Console.WriteLine(); newXmlDoc = null; } } } 这篇关于基于元素计数拆分XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-02 22:40