我需要一个XML文件并从输入文件的重复节点创建多个输出xml文件。源文件 “AnimalBatch.xml” 看起来是这样的:将XML文档拆分为创建重复元素的多个输出文件
<?xml version="1.0" encoding="utf-8" ?>
<Animals>
<Animal id="1001">
<Quantity>One</Quantity>
<Adjective>Red</Adjective>
<Name>Rooster</Name>
</Animal>
<Animal id="1002">
<Quantity>Two</Quantity>
<Adjective>Stubborn</Adjective>
<Name>Donkeys</Name>
</Animal>
<Animal id="1003">
<Quantity>Three</Quantity>
<Color>Blind</Color>
<Name>Mice</Name>
</Animal>
</Animals>
程序需要拆分的重复 “动物”,并产生3个文件命名为:Animal_1001.xml,Animal_1002。 xml和Animal_1003.xml
每个输出文件应该只包含它们各自的元素(这将是根)。 AnimalsBatch.xml中的id属性将为Animal_xxxx.xml文件名提供序列号。 id属性不需要在输出文件中。
Animal_1001.xml:
<?xml version="1.0" encoding="utf-8"?>
<Animal>
<Quantity>One</Quantity>
<Adjective>Red</Adjective>
<Name>Rooster</Name>
</Animal>
Animal_1002.xml
<?xml version="1.0" encoding="utf-8"?>
<Animal>
<Quantity>Two</Quantity>
<Adjective>Stubborn</Adjective>
<Name>Donkeys</Name>
</Animal>
Animal_1003.xml>
<?xml version="1.0" encoding="utf-8"?>
<Animal>
<Quantity>Three</Quantity>
<Adjective>Blind</Adjective>
<Name>Mice</Name>
</Animal>
我想与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 <ENTER>");
Console.ReadLine();
}
我想我有2个问题。
A)在将节点n上的ImportNode转换为outdoc后,我调用outdoc.AppendChild(n),它抱怨:“要插入的节点来自不同的文档上下文。我不知道这是否是在ForEach循环中引用节点n的范围问题 - 或者如果我以某种方式不正确地使用ImportNode()或AppendChild。 ImportNode()的第二个参数设置为true,因为我希望Animal的子元素(任意命名为Quantity,Adjective和Name的3个字段)在目标文件中结束。 B)第二个问题是将Animal元素转化为outdoc。我得到''但我需要'',所以我可以将节点n放入其中。我认为我的问题是我在做什么:outdoc.AppendChild(rootnode);为了显示xml,我在做:outdoc.Save(Console.Out);我确实有将代码保存()到输出文件 - 这确实有效,只要我可以正确组装outdoc。
还有一个类似的问题:Split XML in Multiple XML files,但我不明白的解决方案代码呢。我想我很接近这种方法,并会感谢您提供的任何帮助。
我将使用XmlReader来完成同样的任务,因为我需要能够处理大量的输入文件,而且我知道XmlDocument会读取整个内容并导致内存问题。
在第二prolblem,我说:“我越来越----但需要----”应改为:我越来越 ,但我需要 。我需要一个名为Animal的标签,它带有一个单独的结束标签(一个容器),而不仅仅是一个独立的元素。另外 - 这个类似的问题有它的Java解决方案,我需要C#。 :) –
2012-08-11 05:17:45