Csharp/C#教程:拆分XML文档,从重复元素创建多个输出文件分享


拆分XML文档,从重复元素创建多个输出文件

我需要获取一个XML文件,并从输入文件的重复节点创建多个输出xml文件。 源文件“AnimalBatch.xml”如下所示:




One
Red
Rooster


Two
Stubborn
Donkeys


Three
Blind
Mice

程序需要拆分重复的“Animal”并生成3个名为Animal_1001.xml,Animal_1002.xml和Animal_1003.xml的文件。

每个输出文件应该只包含它们各自的元素(它们是根)。 来自AnimalsBatch.xml的id属性将提供Animal_xxxx.xml文件名的序列号。 id属性不需要在输出文件中。


Animal_1001.xml:


One
Red
Rooster


Animal_1002.xml


Two
Stubborn
Donkeys


Animal_1003.xml>


Three
Blind
Mice

我想用XmlDocument来做这件事,因为它需要能够在.Net 2.0上运行。

我的程序看起来像这样:

  static void Main(string[] args) { string strFileName; string strSeq; XmlDocument doc = new XmlDocument(); doc.Load("D:\Rick\Computer\XML\AnimalBatch.xml"); XmlNodeList nl = doc.DocumentElement.SelectNodes("Animal"); foreach (XmlNode n in nl) { strSeq = n.Attributes["id"].Value; XmlDocument outdoc = new XmlDocument(); XmlNode rootnode = outdoc.CreateNode("element", "Animal", ""); outdoc.AppendChild(rootnode); // Put the wrapper element into outdoc outdoc.ImportNode(n, true); // place the node n into outdoc outdoc.AppendChild(n); // This statement errors: // "The node to be inserted is from a different document context." strFileName = "Animal_" + strSeq + ".xml"; outdoc.Save(Console.Out); Console.WriteLine(); } Console.WriteLine("END OF PROGRAM: Press "); Console.ReadLine(); } 

我想我有两个问题。

A)在节点n上将ImportNode作为outdoc后,我调用outdoc.AppendChild(n),它抱怨:“要插入的节点来自不同的文档上下文。” 我不知道这是否是引用ForEach循环中的节点n的范围问题 – 或者我是否在某种程度上不正确使用ImportNode()或AppendChild。 ImportNode()的第二个参数设置为true,因为我希望Animal的子元素(3个字段任意命名为Quantity,Adjective和Name)最终在目标文件中。

B)第二个问题是将Animal元素变为outdoc。 我得到”但我需要”因此我可以在其中放置节点n。 我认为我的问题是我在做什么:outdoc.AppendChild(rootnode);

为了显示xml,我正在做:outdoc.Save(Console.Out); 我确实有save()到输出文件的代码 – 只要我能正确组装outdoc,它确实有效。

有一个类似的问题: 在多个XML文件中拆分XML ,但我还不了解解决方案代码。 我认为我对这种方法非常接近,并且非常感谢您提供的任何帮助。

我将使用XmlReader执行相同的任务,因为我将需要能够处理大型输入文件,并且我理解XmlDocument读取整个内容并且可能导致内存问题。

这是一个看似你正在寻找的简单方法

 public void test_xml_split() { XmlDocument doc = new XmlDocument(); doc.Load("C:\animals.xml"); XmlDocument newXmlDoc = null; foreach (XmlNode animalNode in doc.SelectNodes("//Animals/Animal")) { newXmlDoc = new XmlDocument(); var targetNode = newXmlDoc.ImportNode(animalNode, true); newXmlDoc.AppendChild(targetNode); newXmlDoc.Save(Console.Out); Console.WriteLine(); } } 

这种方法似乎可以在不使用“var targetnode”语句的情况下工作。 它在ForEach循环中从outdoc的“Animal”元素创建一个名为targetNode的XmlNode对象。 我认为原始代码中出现问题的主要原因是:A)我错误地得到了nodelist nl。 B)我不能“导入”节点n,我认为因为它与doc特别相关。 它必须创建为自己的Node。

先前提出的解决方案的问题是使用“var”关键字。 我的程序必须假设2.0,并且随v3.0推出。 我喜欢罗杰斯的解决方案,因为它简洁明了。 对我来说 – 我想把每件事作为一个单独的陈述。

上述就是C#学习教程:拆分XML文档,从重复元素创建多个输出文件分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

  static void SplitXMLDocument() { string strFileName; string strSeq; XmlDocument doc = new XmlDocument(); // The input file doc.Load("D:\Rick\Computer\XML\AnimalBatch.xml"); XmlNodeList nl = doc.DocumentElement.SelectNodes("//Animals/Animal"); foreach (XmlNode n in nl) { strSeq = n.Attributes["id"].Value; // Animal nodes have an id attribute XmlDocument outdoc = new XmlDocument(); // Create the outdoc xml document XmlNode targetNode = outdoc.CreateElement("Animal"); // Create a separate node to hold the Animal element targetNode = outdoc.ImportNode(n, true); // Bring over that Animal targetNode.Attributes.RemoveAll(); // Remove the id attribute in  outdoc.ImportNode(targetNode, true); // place the node n into outdoc outdoc.AppendChild(targetNode); // AppendChild to make it stick strFileName = "Animal_" + strSeq + ".xml"; outdoc.Save(Console.Out); Console.WriteLine(); outdoc.Save("D:\Rick\Computer\XML\" + strFileName); Console.WriteLine(); } } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1012930.html

(0)
上一篇 2021年12月30日
下一篇 2021年12月30日

精彩推荐