Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi

i developed an application which accept an XML and split that in to small multiple xmls

here below is the code
C#
protected void Button1_Click(object sender, EventArgs e)
{
    string strFileName;
    string strSeq;
    XmlReader doc = XmlReader.Create("D:\\OM\\omwebncomp\\Web\\Vikram Noida\\NearMissRepot_V3_data.xml");
    while (doc.Read())
    {
        if (doc.Name == "MetaData")
        {
            XmlDocument outdoc = new XmlDocument();
            XmlDeclaration xmlDeclaration = outdoc.CreateXmlDeclaration("1.0", "utf-8", null);
            XmlElement rootNode = doc.
            rootNode.InnerXml = doc.ReadInnerXml();
            outdoc.InsertBefore(xmlDeclaration, outdoc.DocumentElement);
            outdoc.AppendChild(rootNode);
            strFileName = "MetaData.xml";
            outdoc.Save("C:\\" + strFileName);
        }
        if (doc.Name == "General")
        {
            XmlDocument outdoc = new XmlDocument();
            XmlDeclaration xmlDeclaration = outdoc.CreateXmlDeclaration("1.0", "utf-8", null);
            XmlElement rootNode = outdoc.CreateElement("AriReport");
            rootNode.InnerXml = doc.ReadInnerXml();
            outdoc.InsertBefore(xmlDeclaration, outdoc.DocumentElement);
            outdoc.AppendChild(rootNode);
            strFileName = "General.xml";
            outdoc.Save("C:\\" + strFileName);
        }
    }
}

problem is that i want to add root node as first line of my original xml.

any one help me
Posted
Updated 28-Mar-13 19:17pm
v2

1 solution

C#
XmlDocument xmlDoc = new XmlDocument();

// Write down the XML declaration
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0","utf-8",null);

// Create the root element
XmlElement rootNode  = xmlDoc.CreateElement("CategoryList");
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
xmlDoc.AppendChild(rootNode);

// continue adding your elements to "rootNode"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900