基于asp.net中的子节点值拆分XML文件

时间:2012-05-02 07:16:13

标签: asp.net xml dom

我想在子节点的基础上拆分XML文件,并显示拆分文件。

1 个答案:

答案 0 :(得分:0)

虽然您尚未提供完整信息,但以下是您要求附近的示例代码。

它读取main.xml文件,其中包含许多名为ChildNode的子节点,并为每个子节点写入单独的文件。

XmlDocument xml= new XmlDocument();
xml.Load(@"c:\main.xml");

XmlNodeList xnRep = xml.DocumentElement.GetElementsByTagName("ChildNode");

string strFileName = "ChildNode";
int intFileCount;

for(int i =0; i <xnRep.Count;i++)
{
          //Stores the ChildNode Elements in the Variable
          strDest = xnRep[i].InnerXml;

          //Create the New File with the name "ChildNode_1.xml" and "ChildNode_3.xml" 
          XmlWriter xw = XmlWriter.Create(strFileName + "_" + intFileCount + ".xml");

          //Write the XML
          xw.WriteRaw(strDest.ToString());

          xw.Close();

          intFileCount++;
}

您还可以在split xml document into chunks

查看类似的答案
相关问题