基于元素计数拆分XML文件 [英] Split XML File based on element count
本文介绍了基于元素计数拆分XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
i有一个大的XML文件,我希望根据元素计数分成几个文件
Ex。如果文件如下所示
Hello,
i have a big XML file,which i want to split in several files based on Element count
Ex. 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?
推荐答案
在以下链接上尝试逻辑它可能有效:)
http://stackoverflow.com/问题1 ons / 12016946 / how-to-split-an-xml-file-into-multiple-xml-files [ ^ ]
< a href =http://stackoverflow.com/questions/14455639/how-to-split-an-xml-file-into-multiple-xml-files-based-on-nodes> http://stackoverflow.com / questions / 14455639 / how-to-split-an-xml-file-into-multiple-xml-files-based-on-nodes [ ^ ]
Try logic on following links it might work :)
http://stackoverflow.com/questions/12016946/how-to-split-an-xml-file-into-multiple-xml-files[^]
http://stackoverflow.com/questions/14455639/how-to-split-an-xml-file-into-multiple-xml-files-based-on-nodes[^]
我这样做了
i did it this way
//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文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文