Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a Xml document as,
HTML
'<?xml version="1.0" encoding="utf-8"?><root>some text here<pre lang="text"></pre><\root>'

I want my XML to be place inside a new tag as follows,
HTML
<data>

<?xml version="1.0" encoding="utf-8"?><root>some text here<pre lang="text"></pre><\root>

</data>

What would be the perfect solution for this.

Thanks in Advance.
Posted
Updated 9-Oct-12 2:57am
v2

You can use CDATA.

XML
<data>
<![CDATA[
<?xml version="1.0" encoding="utf-8"?><root>some text here<pre lang="text">

]]>
 
Share this answer
 
Comments
Member 9353131 9-Oct-12 9:14am    
I need .NET code to work on this.
C#
public static void Main()
 {
   XmlDocument docA = new XmlDocument();
   XmlDocument docB = new XmlDocument();

   docA.LoadXml("<?xml version=""1.0"" encoding=""utf-8""?><root>some text here<pre lang=""text""></pre></root>");
   docB.loadXml("<?xml version=""1.0"" encoding=""utf-8""?><data></data>");

    System.IO.StringWriter sw;
    sw = new System.IO.StringWriter();
    docA.Save(sw);
   //Create a CData section.
   XmlCDataSection CData;
   CData = docB.CreateCDataSection(sw.ToString());

   //Add the new node to the document.
   XmlElement root = docB.DocumentElement;
   root.AppendChild(CData);

   docB.save("C:\\result.xml");

 }
 
Share this answer
 
Try following code

C#
XmlDocument origXml = new XmlDocument();
origXml.Load("YourXml.Xml");

XmlDocument newXml = new XmlDocument();
newXml.LoadXml("<data></data>");


XmlNode rootNode= newXml.ImportNode(origXml.DocumentElement, true);

newXml.DocumentElement.AppendChild(rootNode);


Hope that helps. If it does, mark it as answer/upvote.
-Milind
 
Share this answer
 
XML
Here's the XML File:

<?xml version="1.0" encoding="utf-8"?>
<CategoryList>
  <Category ID="01">
    <MainCategory>XML</MainCategory>
    <Description>This is a list my XML articles.</Description>
    <Active>true</Active>
  </Category>
</CategoryList>
Here's the code:

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml" %>
<%@ Page Language="C#" Debug="true" %>

<script  runat="server">
void Page_Load(object sender, System.EventArgs e){
    if(!Page.IsPostBack){
        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);

        // Create a new <Category> element and add it to the root node
        XmlElement parentNode  = xmlDoc.CreateElement("Category");

        // Set attribute name and value!
        parentNode.SetAttribute("ID", "01");

        xmlDoc.DocumentElement.PrependChild(parentNode);

        // Create the required nodes
        XmlElement mainNode  = xmlDoc.CreateElement("MainCategory");
        XmlElement descNode  = xmlDoc.CreateElement("Description");
        XmlElement activeNode  = xmlDoc.CreateElement("Active");

        // retrieve the text
        XmlText categoryText= xmlDoc.CreateTextNode("XML");
        XmlText descText  = xmlDoc.CreateTextNode("This is a list my XML articles.");
        XmlText activeText  = xmlDoc.CreateTextNode("true");

        // append the nodes to the parentNode without the value
        parentNode.AppendChild(mainNode);
        parentNode.AppendChild(descNode);
        parentNode.AppendChild(activeNode);

        // save the value of the fields into the nodes
        mainNode.AppendChild(categoryText);
        descNode.AppendChild(descText);
        activeNode.AppendChild(activeText);

        // Save to the XML file
        xmlDoc.Save( Server.MapPath("categories.xml"));

        Response.Write("XML file created");
    }
}
</script>
 
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